Best way to export a Python sqlite table to .csv file - python

I have a simple single table in Python 2.7 sqllite. I just want to port the table to a external .csv file.
Been reading a few tutorials and they are writing gobs and gobs of code to do this.
Seems like this would be a simple method to call up the table ('Select * FROM Table') and save it to .csv.
Thanks

You could also use the csv module for output, especially if your string fields contain commas.
#!/usr/bin/python3
import sqlite3
connection = sqlite3.connect('example_database')
cursor = connection.cursor()
cursor.execute('drop table example_table')
cursor.execute('create table example_table(string varchar(10), number int)')
cursor.execute('insert into example_table (string, number) values(?, ?)', ('hello', 10))
cursor.execute('insert into example_table (string, number) values(?, ?)', ('goodbye', 20))
cursor.close()
cursor = connection.cursor()
cursor.execute('select * from example_table')
for row in cursor.fetchall():
print('{},{}'.format(row[0], row[1]))
cursor.close()
connection.close()

Related

Trying to insert data into SQL database. Error 1046 No Database Selected

I have some data in a tab-delimited file that i'm trying to insert into an SQL database. Here is my code
import csv
import MySQLdb
new_db = MySQLdb.connect(host='localhost',user='me')
cursor = new_db.cursor()
cursor.execute('create database if not exists my_database')
maketablestr = """CREATE TABLE info (Disease VARCHAR(10), NCBI Acc. Number VARCHAR(10) ,Length VARCHAR(10), Sequence VARCHAR(10));"""
cursor.execute(maketablestr)
new_db.commit()
tsv_read = csv.reader(file('marfan.tsv'), delimiter = '\t')
for row in tsv_read:
cursor.execute('INSERT INTO info(Disease, NCBI Acc. Number, Length, Sequence ) VALUES(%s, %s, %s, %s)', row)
new_db.commit()
cursor.close()
new_db.close()
print("Database updated")
When I run the code, it gives me the error 1046 'No Database Selected'. I'm a little confused as to why I'm getting this error since the code that I wrote is mostly taken from others who are trying to do the same thing as me.
There are two approaches to solve this:
You run cursor.execute('use my_database;')
Or you adapt your SQL statements to specify the DB like:
cursor.execute('INSERT INTO my_database.info(Disease, NCBI Acc. Number, Length, Sequence ) VALUES(%s, %s, %s, %s)', row)
Note, with the later approach you need to adapt all sql statements

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 :)

Python and PostgreSQL - GeomFromText

I'm a Python learner,
I'm trying to insert geometry records into PostgreSQL.
If I tried the query without the geometry column, it works fine and all data inserted successfully.
cur.execute("INSERT INTO taxi (userid,carNum) SELECT '"+str(msg['UserID'])+"',"+str(msg['CarNumber']))
Once I try to add the geometry records, nothing happens! execution ends without errors but nothing being inserted into DB.
cur.execute("INSERT INTO taxi (position,userid,carNum) SELECT GeomFromText('POINT("+str(float(msg['longitude']))+" "+str(float(msg['latitude']))+")',4326),'"+str(msg['UserID'])+"',"+str(msg['CarNumber']))
Couldn't figure out what I'm missing here
You need to commit the data to the database.
Check the documentation of psycopg2 http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries
Follow those steps
>>> import psycopg2
# Connect to an existing database
>>> conn = psycopg2.connect("dbname=test user=postgres")
# Open a cursor to perform database operations
>>> cur = conn.cursor()
# Execute a command: this creates a new table
>>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
# Pass data to fill a query placeholders and let Psycopg perform
# the correct conversion (no more SQL injections!)
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
... (100, "abc'def"))
# Query the database and obtain data as Python objects
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchone()
(1, 100, "abc'def")
# Make the changes to the database persistent
>>> conn.commit()
# Close communication with the database
>>> cur.close()
>>> conn.close()

Saving data from MySQL table to a text file with Python

So I have a mysql table and I am trying to take each element from one of the fields of the table. The field and table are both called "keywords". In the field there are many different random words and I am trying to take all of those and save them to a text file. Any help on how to implement this would be great, here is what I have so far.
#!/usr/bin/env python
import MySQLdb
db = MySQLdb.connect(host="", user="", passwd="", db="")
cursor = db.cursor()
sql = """SELECT DISTINCT keywords FROM keywords"""
cursor.execute(sql)
cursor.fetchall()
db.close()
for s in sql:
tweets = open("keywords.txt", "w")
What I was thinking is to turn what sql fetches into a list if possible and write that to the file. But I am open to any suggestions, thanks.
Something like this should work:
import MySQLdb
db = MySQLdb.connect(host="", user="", passwd="", db="")
cursor = db.cursor()
sql = """SELECT DISTINCT keywords FROM keywords"""
tweets = open("keywords.txt", "w")
cursor.execute(sql)
for row in cursor:
print>>tweets, row[0]
tweets.close()
db.close()

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