Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, Sqlite not saving results on the file

I have this code in Python:

conn = sqlite3.connect("people.db")
cursor = conn.cursor()

sql = 'create table if not exists people (id integer, name VARCHAR(255))'
cursor.execute(sql)
conn.commit()

sql = 'insert into people VALUES (3, "test")'
cursor.execute(sql)
conn.commit()   

sql = 'insert into people VALUES (5, "test")'
cursor.execute(sql)
conn.commit()  

print 'Printing all inserted'
cursor.execute("select * from people")
for row in cursor.fetchall():
    print row

cursor.close()
conn.close()

But seems is never saving to the database, there is always the same elements on the db as if it was not saving anything.

On the other side If I try to access the db file via sqlite it I got this error:

Unable to open database "people.db": file is encrypted or is not a database

I found on some other answers to use conn.commit instead of conn.commit() but is not changing the results.

Any idea?

like image 885
Mc- Avatar asked Aug 04 '12 10:08

Mc-


People also ask

How does Python store data in SQLite?

First, connect to the SQLite database by creating a Connection object. Second, create a Cursor object by calling the cursor method of the Connection object. Third, execute an INSERT statement. If you want to pass arguments to the INSERT statement, you use the question mark (?) as the placeholder for each argument.

Where does SQLite store data?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.

Does SQLite auto commit?

SQLite transaction statementsBy default, SQLite operates in auto-commit mode. It means that for each command, SQLite starts, processes, and commits the transaction automatically. To start a transaction explicitly, you use the following steps: First, open a transaction by issuing the BEGIN TRANSACTION command.


2 Answers

This seems to work alright for me ("In database" increases on each run):

import random, sqlite3

conn = sqlite3.connect("people.db")
cursor = conn.cursor()

sql = 'create table if not exists people (id integer, name VARCHAR(255))'
cursor.execute(sql)

for x in xrange(5):
    cursor.execute('insert into people VALUES (?, "test")', (random.randint(1, 10000),))
conn.commit()

cursor.execute("select count(*) from people")
print "In database:", cursor.fetchone()[0]
like image 38
AKX Avatar answered Sep 26 '22 14:09

AKX


BINGO ! people! I Had the same problem. One of thr reasons where very simple. I`am using debian linux, error was

Unable to open database "people.db": file is encrypted or is not a database

database file was in the same dir than my python script connect line was
conn = sqlite3.connect('./testcases.db')

I changed this

conn = sqlite3.connect('testcases.db')

! No dot and slash. Error Fixed. All works

If someone think it is usefull, you`re welcome

like image 59
user3252247 Avatar answered Sep 24 '22 14:09

user3252247