Problema de SQLite3 en Python

I am trying to do some basic iterating over a database in Python. I am using a table that already exists and iterating some int values into it. Pretty simple stuff but I can't seem to get it working. Here is my non-functional code.

When I run the code it doesn't return any errors, but when I check the table in the database, nothing was written to the row in the table called "numbers".

import sqlite3

conn = sqlite3.connect("mydbase.sqlite")
c = conn.cursor()

for x in range(21):
  c.execute("""insert into numbers values (?)""", (x,))

conn.commit()
c.close()

preguntado el 27 de agosto de 11 a las 23:08

Have you ever tried using executemany()? -

After creating a sqlite database with the sqlite3 utility and running create table numbers (foo integer);, your code works fine for me (numbers 0 to 20 get inserted into the numbers table). Are you SURE you're looking at the right file? -

1 Respuestas

The code as written will work assuming you are actually connecting to your database. Are you sure you are connecting to your database? Right CWD?

This is a shell session showing it working

Make a database:

(~/): 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

Connect to that DB and update it in 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()
>>>

Confirm that it worked:

(~/): 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

Para su información

conn = sqlite3.connect("NOT_THERE.sqlite") will not explode. Your first error will show up when you execute the insert and don't have a table in the newly created NOT_THERE database.

Respondido el 10 de Septiembre de 11 a las 22:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.