Psycopg2 command hangs, possibly because of table name? - python

The code below hangs (even ctrl-C won't stop it, I have to close the terminal) when it tries to create the second table, and I'm not sure why. The first table is created successfully (I can see it in psql with \dt cyanobacteria.*). A simple solution would be to rename the table, but I'm trying to restore someone else's code to working order and I'd have to go through changing lots of stuff. And he had it working once, so it ought to work for me!
I've created a database called 'genomes', a user called 'genomes_admin' and a schema called 'cyanobacteria'. Then I try to make some tables:
#!/usr/bin/python
import psycopg2
psql = "dbname='genomes' user='genomes_admin'"
schm = 'cyanobacteria'
conn = psycopg2.connect(psql)
cur = conn.cursor()
cur.execute('''SET search_path TO %s''', (schm,))
conn.commit()
cur.execute('''CREATE TABLE IF NOT EXISTS testnm(blah text, length int) ''')
print 'created testnm'
conn.commit()
print 'committed'
cur.execute('''CREATE TABLE IF NOT EXISTS genomes(blah text, length int) ''') # hangs here
print 'created genomes' # this line never executes
conn.commit()
print 'committed'
cur.close()
conn.close()

Related

Python sqlite3 conn.commit() not saving data to the db file

Here's my code
import sqlite3
conn = sqlite3.connect('arbitrary.db')
c = conn.cursor()
data = ['sampletext',42]
c.execute("CREATE TABLE IF NOT EXISTS t1(link TEXT,story_id INTEGER)")
c.execute("INSERT INTO t1 (link, story_id) VALUES (?,?)", data)
conn.commit()
conn.close()
It runs without error and doesn't throw any parsing errors, but once the script closes and I go to open the db file, It's still empty. am I making a mistake somewhere?
Appreciate the help in advance :)

Cannot operate on a closed cursor in sqlite3 python

I've been following this 5 part tutorial from this YouTube playlist: https://www.youtube.com/playlist?list=PLQVvvaa0QuDezJh0sC5CqXLKZTSKU1YNo
I'm using Jupyter notebook with Python with the following code:
import sqlite3
import time
import datetime
import random
conn = sqlite3.connect("tutorial2.db")
c = conn.cursor()
Then I create several functions.
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS stuffToPlot (unix REAL, datestamp TEXT,
keyword TEXT, value REAL)')
def data_entry():
c.execute("INSERT INTO stuffToPlot VALUES (145123542, '2016-01-03',
'Python', 7)")
conn.commit()
c.close()
conn.close()
create_table()
data_entry()
It works fine the first time, and generates a db file in C:\Users\Michael
However, when I try to run only the create_table() function again, I get the following error:
ProgrammingError: Cannot operate on a closed cursor.
Anyone able to help resolving this issue would be greatly appreciated!
The error is pretty explicit: You cannot run queries on closed cursors. Here it's even worse since you have also closed the connection at the first call of the data_entry() function.
I would advise on opening a cursor for each query, and then closing it after completing the query, and only closing the connection at the end of your script:
import sqlite3
import time
import datetime
import random
conn = sqlite3.connect("tutorial2.db")
def create_table():
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS stuffToPlot (unix REAL, datestamp TEXT, keyword TEXT, value REAL)')
c.close()
def data_entry():
c = conn.cursor()
c.execute("INSERT INTO stuffToPlot VALUES (145123542, '2016-01-03', 'Python', 7)")
conn.commit()
c.close()
create_table()
data_entry()
By moving the conn.close() statement after you have completed all of your queries, and opening the cursors only when you need them, the error won't occur anymore.
EDIT : What is happening in your video is the following:
He first executes the whole script once.
He comments the line that creates the table.
He executes the whole script a second time.
I think you are probably entering the commands in a python interactive session, which is not equivalent to what he is doing in the video, because when he re-executes the script, a new connection and cursor are created, whereas if you're only trying to call the function again, the cursor and connection are already closed, which causes the error.

SQLite-Manager on Firefox and Python

I have written a small test application using SQLite with Python 3.3:
import sqlite3
MDB = sqlite3.connect('D:\MDB.db') # create the db object
cursor = MDB.cursor() # assign a cursor
cursor.execute('''CREATE TABLE IF NOT EXISTS section (
Code INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Description TEXT )
''')
cursor.execute('''DELETE FROM section''') # delete contents for reruns
cursor.execute('''INSERT INTO section
(Description)
VALUES (?)
''', ('Abdul, Paula',))
cursor.execute('''INSERT INTO section
(Description)
VALUES (?)
''', ('ABWH',))
print('Results:\n')
cursor.execute('''SELECT * FROM section''')
selection = cursor.fetchall()
for row in selection:
print('\t', row)
The SELECT statement shows the results expected (seeming to indicate that the row exists), but if I connect to the database with SQLite-Manager, the table exists but is empty, and if I try the same query with another script connected to the database, nothing is returned. Can anyone please explain what I am doing wrong?
You're not saving changes (calling MDB.commit).

How To Connect To Multiple sqlite3 Database with Python

this is somewhat related to my earlier query..
Reading A Big File With Python
The problem was with runtime, so i was suggested to use sqlite3 database, and it reduced the time to millisecond, and I am very happy, now the only problem i have is, connecting to different database files in the same folder. All the database files have the same tables.
The code I am using, reads only the first one, and doesnt seem to check the other databases.
The output is when the teacher, enters students ID, it is supposed to return the related records if found in the database table.
my Code is something like this, But I am sure I am doing something wrong, pardon me if its a silly one, as using sqlite3 for the first time.
#other codes above not related to this part
databases = []
directory = "./Databases"
for filename in os.listdir(directory):
flname = os.path.join(directory, filename)
databases.append(flname)
for database in databases:
conn = sqlite3.connect(database)
conn.text_factory = str
cur = conn.cursor()
sqlqry = "SELECT * FROM tbl_1 WHERE std_ID='%s';" % (sudentID)
try:
c = cur.execute(sqlqry)
data = c.fetchall()
for i in data:
print "[INFO] RECORD FOUND"
print "[INFO] STUDENT ID: "+i[1]
print "[INFO] STUDENT NAME: "+i[2]
#and some other info
conn.close()
except sqlite3.Error as e:
print "[INFO] "+e
Thanks For Any guides
#Whiskey, sometimes it helps to try to break the problem down into a minimal example and see if that works or where it breaks. Since you are able to see the database names being printed as they are opened, my guess would be a problem with the query or possibly the data in the db even tho the records seem to be there. When you say it doesn't find the record you're looking for does it just print out nothing or does it print out the "[INFO]" line in your exception handler?
I put together the following minimal example, and it seems to be working as far as my understanding of your problem goes. My only other piece of advice to add to everyone else's would be to parametrize your query rather than using the raw input directly to make your app a little more secure. Hope it helps:
import os, sqlite3
"""
Create the test databases:
sqlite3 Databases/test_db1.db
sqlite> CREATE TABLE foo ( id INTEGER NOT NULL, name VARCHAR(100), PRIMARY KEY (id) );
sqlite>
sqlite3 Databases/test_db2.db
sqlite> CREATE TABLE foo ( id INTEGER NOT NULL, name VARCHAR(100), PRIMARY KEY (id) );
sqlite> INSERT INTO foo VALUES (2, 'world');
"""
databases = []
student_id = 2
directory = "./Databases"
for filename in os.listdir(directory):
flname = os.path.join(directory, filename)
databases.append(flname)
for database in databases:
try:
with sqlite3.connect(database) as conn:
conn.text_factory = str
cur = conn.cursor()
sqlqry = "SELECT * FROM foo WHERE id=:1;"
c = cur.execute(sqlqry, [student_id])
for row in c.fetchall():
print "-- found: %s=%s" % (row[0], row[1])
except sqlite3.Error, err:
print "[INFO] %s" % err

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?
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
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]
You should commit after making changes i.e:
myDatabase.commit()
can you open the db with a tool like sqlite administrator ? this would proove thedb-format is ok.
if i search for that error the solutions point to version issues between sqlite and the db-driver used. maybe you can chrck your versions or AKX could post the working combination.
regards,khz

Categories

Resources