|
Русские буквы Sqlite3 Python
|
|||
---|---|---|---|
#18+
Вопрос: 1) Почему при запуске второго скрипта он ругается на кодировку русских букв и не показывает базу данных? (Если в третьей колонке ввести англискими буквами то все отображается). 2) Как сделать чтобы в базу данных можно было добавлять и читать из неё значения колонок занесенные русскими буквами? Написал использовать кодировку utf-8 1) Скрипт создания базы данных в третьей колонке значение русскими буквами # -*- coding: utf-8 -*- import sqlite3 con = sqlite3.connect('books.db') cur = con.cursor() cur.execute('CREATE TABLE books (id INTEGER PRIMARY KEY, title VARCHAR(100), author VARCHAR(30))') cur.execute('INSERT INTO books (id, title, author) VALUES(1, "Mathematics", "Иванов")') cur.execute('INSERT INTO books (id, title, author) VALUES(2, "lirika", "Петров")') cur.execute('INSERT INTO books (id, title, author) VALUES(3, "Astronomija", "Сидоров")') con.commit() cur.close() 2) Скрипт добавления и просмотра: # -*- coding: utf-8 -*- import string,cgi,sqlite3 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer page0=""" <b>menu</b><rb> <form action="" method="post" > <input type="submit" value="send post" > </form> <form action="sql" method="get" > <input type="submit" value="books base" > </form> """ page_start=""" <b>books</b><br> <table border=1 width=600> <tr><td><b>id</b></td><td><b>title</b></td><td><b>author</b></td></tr> """ page_end=""" </table> <form action="sql" method="get" > <input type="submit" value="show all" > </form> <form action="send_select" method="get" > id: <input name="id" type=text size=3> <input type="submit" value="select" > </form> <form action="send_delete" method="get" > id: <input name="id" type=text size=3> <input type="submit" value="delete" > </form> <form action="send_put" method="get" > id: <input name="id" type=text> title: <input name="title" type=text> author: <input name="author" type=text> <input type="submit" value="put" > </form> """ def show_base(self): page=page_start con = sqlite3.connect('books.db') cur = con.cursor() cur.execute('SELECT * FROM books') for row in cur: page+='<tr><td>'+str(row[0])+'</td><td>'+row[1]+'</td><td>'+row[2]+'</td><tr>' page+=page_end cur.close() self.wfile.write(page) class MyHandler(BaseHTTPRequestHandler): def do_GET(self): plist = self.path.split("?") self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() if plist[0]=='/' : self.wfile.write(page0) if plist[0]=='/send_put' : pl=plist[1].split("&") #split values l1=pl[0].split('=') #get id value l2=pl[1].split('=') #get title value l3=pl[2].split('=') #get author value data=(l1[1],l2[1],l3[1]) con = sqlite3.connect('books.db') cur = con.cursor() cur.execute('INSERT INTO books (id, title, author) VALUES(?, ?, ?)',data) con.commit() cur.close() show_base(self) if plist[0]=='/sql' : show_base(self) def main(): server = HTTPServer(('', 80), MyHandler) print 'started httpserver...' server.serve_forever() if __name__ == '__main__': main() ... |
|||
:
Нравится:
Не нравится:
|
|||
07.07.2009, 14:02 |
|
Русские буквы Sqlite3 Python
|
|||
---|---|---|---|
#18+
А если сделать так? sidalexsandr cur.execute(u'INSERT INTO books (id, title, author) VALUES(1, "Mathematics", "Иванов")') cur.execute(u'INSERT INTO books (id, title, author) VALUES(2, "lirika", "Петров")') cur.execute(u'INSERT INTO books (id, title, author) VALUES(3, "Astronomija", "Сидоров")') Может быть, язык стоит осваивать с чтения туториала, а не написания приложения бд? ... |
|||
:
Нравится:
Не нравится:
|
|||
21.07.2009, 22:38 |
|
|
start [/forum/topic.php?fid=54&fpage=29&tid=2009441]: |
0ms |
get settings: |
8ms |
get forum list: |
12ms |
check forum access: |
3ms |
check topic access: |
3ms |
track hit: |
45ms |
get topic data: |
14ms |
get forum data: |
3ms |
get page messages: |
70ms |
get tp. blocked users: |
2ms |
others: | 16ms |
total: | 176ms |
0 / 0 |