Command to execute sqlite select statement in python - python

I have below sqlite query
SELECT EMP_CODE, FullName FROM EmpMaster
I have below code in python
I will then create CSV/xlsx file on the data extracted. Kindly provide python code for select statement.

You can execute SQL statements directly python,
import mysql.connector
mydb = mysql.connector.connect(host="localhost",user="yourusername",password="yourpassword",database="mydatabase")
mycursor = mydb.cursor()
mycursor.execute("SELECT EMP_CODE, FullName FROM EmpMaster")
myresult = mycursor.fetchall()
for more on mysql check this link

Related

SQLITE with python : Adding the database name in SELECT statement

I am using sqlite3 with python, and after connecting to the database and creating a table, sqlite3 shows an error when I try to execute a SELECT statment on the table with the name of the databse in it :
con = sqlite3.connect("my_databse")
cur = con.cursor()
cur.execute('''CREATE TABLE my_table ... ''')
cur.execute("SELECT * FROM my_database.my_table") # this works fine without the name of the database before the table name
but I get this error from sqlite3 :
no such table : my_database.my_table
Is there a way to do a SELECT statment with the name of the database in it ?
The short answer is no you can't do this with SQLite. This is because you already specify the database name with sqlite3.connect() and SQLite3 doesn't allow multiple databases in the same file.
Make sure of the database is in the same directory with the python script. In order to verify this you can use os library and os.listdir() method. After connecting the database and creating the cursor, you can query with the table name.
cur.execute('SELECT * FROM my_table')

Importing .db file into Postgresql database

I am currently working on a script to import a .db file into Postgresql database, including the data. Is there any way to do so without using third party tools and by using python?
You can do it with Django for sure.
python manage.py dumpdata > db.json
Change the database settings to new database such as of PostgreSQL.
python manage.py migrate
python manage.py shell
Enter the following in the shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
python manage.py loaddata db.json
Otherwise if you want to tinker your way.
You need to install psycopg2
$ pip install psycopg2
Then you connect to Postgres.
import psycopg2
conn = psycopg2.connect("host=localhost dbname=postgres user=postgres")
This is how you insert values.
cur = conn.cursor()
insert_query = "INSERT INTO users VALUES {}".format("(10, 'hello#dataquest.io', 'Some Name', '123 Fake St.')")
cur.execute(insert_query)
conn.commit()
Now with SQLAlchemy you can easily open an SQLite file.
import sqlite3
conn = sqlite3.connect('database.db')
Fetch the data.
r = conn.execute("""SELECT * FROM books""")
r.fetchall()
Here is how to fetch all tables from SQLite
all the table names within your database:
SELECT name FROM sqlite_master WHERE type = 'table'
sqlite_master can be thought of as a table that contains information about your databases (metadata).
A quick but most likely inefficient way (because it will be running 700 queries with 700 separate resultsets) to get the list of table names, loop through those tables and return data where columnA = "-":
for row in connection.execute('SELECT name FROM sqlite_master WHERE type = "table" ORDER BY name').fetchall()
for result in connection.execute('SELECT * FROM ' + row[1] + ' WHERE "columnA" = "-"').fetchall()
# do something with results
Here is an other approach
import sqlite3
try:
conn = sqlite3.connect('/home/rolf/my.db')
except sqlite3.Error as e:
print('Db Not found', str(e))
db_list = []
mycursor = conn.cursor()
for db_name in mycursor.execute("SELECT name FROM sqlite_master WHERE type = 'table'"):
db_list.append(db_name)
for x in db_list:
print "Searching",x[0]
try:
mycursor.execute('SELECT * FROM '+x[0]+' WHERE columnA" = "-"')
stats = mycursor.fetchall()
for stat in stats:
print stat, "found in ", x
except sqlite3.Error as e:
continue
conn.close()

Using python and sqlite3 how do I open a file?

I understand how to open a database using a python script:
import sqlite3 as lite
.
.
con = lite.connect(db_name)
However what I have is a lot of database files made with tsk_loaddb, when I am in sqlite3 I can use the following
SQLite version 3.8.7.2 2014-11-18 20:57:56
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open /work/jmjohnso1/db_project/db_IN60-1020
sqlite> .tables
blackboard_artifact_types tsk_files_derived_method
blackboard_artifacts tsk_files_path
blackboard_attribute_types tsk_fs_info
blackboard_attributes tsk_image_info
tsk_db_info tsk_image_names
tsk_file_layout tsk_objects
tsk_files tsk_vs_info
tsk_files_derived tsk_vs_parts
However, I am not understanding how to do it in python. My question is how do I translate the open command with the sqlite3 python library.
Turns out I had a newline in the name because I was reading from a file. So the following works.
def open_sql(db_folder, db_name, table):
# databases are located at /work/jmjohnso1/db_project
path_name = os.path.join(db_folder,db_name).strip()
con = lite.connect(path_name)
cur = con.cursor()
cur.execute('SELECT * FROM ' + table)
col_names = [cn[0] for cn in cur.description]
rows = cur.fetchall()
print_header(col_names)
print_output(rows)
con.close()

using python 2.7 to query sqlite3 database and getting "sqlite3 operational error no such table"

My simple test code is listed below. I created the table already and can query it using the SQLite Manager add-in on Firefox so I know the table and data exist. When I run the query in python (and using the python shell) I get the no such table error
def TroyTest(self, acctno):
conn = sqlite3.connect('TroyData.db')
curs = conn.cursor()
v1 = curs.execute('''
SELECT acctvalue
FROM balancedata
WHERE acctno = ? ''', acctno)
print v1
conn.close()
When you pass SQLite a non-existing path, it'll happily open a new database for you, instead of telling you that the file did not exist before. When you do that, it'll be empty and you'll instead get a "No such table" error.
You are using a relative path to the database, meaning it'll try to open the database in the current directory, and that is probably not where you think it is..
The remedy is to use an absolute path instead:
conn = sqlite3.connect('/full/path/to/TroyData.db')
You need to loop over the cursor to see results:
curs.execute('''
SELECT acctvalue
FROM balancedata
WHERE acctno = ? ''', acctno)
for row in curs:
print row[0]
or call fetchone():
print curs.fetchone() # prints whole row tuple
The problem is the SQL statment. you must specify the db name and after the table name...
'''SELECT * FROM db_name.table_name WHERE acctno = ? '''

pyodbc path in sql statement not working

I`m quite new to python and would like to copy a table from one mdb to another mdb using pyodbc. There seems to be a problem with the paths if a Foldername starts with a digit. I googled for an hour now and couldn't find a solution:
DBfile = r"W:\path\1020 Folder\MDB1.mdb"
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ='+DBfile1)
cursor = conn.cursor()
sql = """SELECT Table1.* INTO test FROM [W:\path\A 1020 Folder\MB2.mdb].Table1;"""
sql1 = """SELECT Table1.* INTO test FROM [W:\path\1020 Folder\MB2.mdb].Table1;"""
cursor.execute(sql) #WORKING
cursor.execute(sql1) #NOT WORKING
conn.commit()
Thanks alot, Achim
You must be very careful when you want to use backshlash \ in strings. You can escape those using \\:
sql1 = """SELECT Table1.* INTO test FROM [W:\\path\\1020 Folder\\MB2.mdb].Table1;"""
You can also use raw string just like you did it with DBfile

Categories