寫入的代碼將工作,假設您實際上連接到您的數據庫。你確定你連接到你的數據庫嗎?正確的CWD?
這是顯示它的工作
做一個數據庫中的shell會話:
(~/): sqlite3 mydbase.sqlite
SQLite version 3.7.2
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE "numbers" ("num" integer);
sqlite> .q
連接到數據庫,並在Python更新:
(~/): python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> conn = sqlite3.connect("mydbase.sqlite")
>>> c = conn.cursor()
>>> for x in range(21):
... c.execute("""INSERT INTO numbers VALUES (?)""", (x,))
...
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
<sqlite3.Cursor object at 0xb76e2c20>
>>> conn.commit()
>>> c.close()
>>>
確認它的工作:
(~/): sqlite3 mydbase.sqlite
SQLite version 3.7.2
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> SELECT * FROM "numbers";
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sqlite>
sqlite> .q
FYI
conn = sqlite3.connect("NOT_THERE.sqlite")
不會爆炸。當您執行插入操作時,您的第一個錯誤將顯示,並且在新創建的NOT_THERE數據庫中沒有表格。
你有沒有嘗試過使用'executemany()'? –
用sqlite3實用程序創建一個sqlite數據庫並運行'create table numbers(foo integer);'後,你的代碼對我來說工作正常(數字0到20被插入'numbers'表中)。你確定你正在查看正確的文件嗎? –