How to populate user input strings to sqlite - python-3.x

I have this code that simply creates a list from user input. I want to load this into sqlite Db instead of list shown but am not conversant with Sqlite. please help
HERE IS THE CODE
listQ = []
while True:
read = input("Type in a line. ").lower().split()
for item in read:
listQ.append( input("Type in a line. ") )
for line in listQ:
import sqlite3
conn = sqlite3.connect('/C/project/new/sqlite_file.db')
c = conn.cursor()
for item in listQ:
c.execute('insert into tablename values (?,?,?)', item)
#print(line)

Related

Printing the right list items in Python from an SQL database

Python reading my textfile with keywords for each line and then I let SQL select it and fetch it in Python, but I don't know the right list comprehension or code to exclude the keywords which couldn't be read by SQL. It only prints the last word from the loop and I want both keywords to be included.
So I have these keywords in a random textfile:
Bohemian Rhapsody
You're
Thriller
Just some random words
The database found tracks for the first two but didn't for the 3rd and 4th line in the file . I want a print statement which says: --- No tracks found for Thriller, Just some random words ---
My code:
import sqlite3, sys
conn = sqlite3.connect(r'C:\Users\Just\Downloads\chinook.db')
cur = conn.cursor()
import_file = input ('Enter file name: ')
with open(import_file, 'r') as f:
unfiltered = f.read().splitlines()
keywords = [filter_empty for filter_empty in unfiltered if filter_empty]
for keyword in keywords:
cur.execute('''SELECT tracks.TrackId, tracks.Name, artists.Name
FROM tracks
INNER JOIN albums ON tracks.AlbumId = albums.AlbumId
INNER JOIN artists ON albums.ArtistId = artists.ArtistId
WHERE tracks.name LIKE (?||'%') ''',(keyword,))
found_tracks = cur.fetchall()
unknown_tracks = []
if len(found_tracks) == 0:
print (keyword)
unknown_tracks += [keyword]
If keyword is not found in the database, the result of this cur.fetchall() will be an empty list. Add a test for that condition and output the desired message.

Python3 Sqllite Normal query works but not while passing it a tuple

I have a Db from where I have to display all the columns which match the substring of the column given by user.
The following code works:
c.execute("select *from Transactions where Description like '%sh%' ")
conn.commit()
print(c.fetchall())
conn.close()
But when I try to run this code it returns me an empty list:
def search(col,val):
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute("Select *from Transactions where ? Like ? ",(col,'%'+val+'%'))
print(c.fetchall())
search('description',"sh")
Also the result will always be a blank list even if the col name is wrong. as opposed the usual error which says column not found.
Please Help

Python insert string with random mixture of " and ' to database

I'm running the linux terminal command 'strings' on a file and storing the result, which is a series of readable strings, in a variable. The results are also written to a text file. I then need to upload the results to a database. The problem is that the result often contains ' and " characters in an unpredictable order. These cause an SQL error. I've tried to string.replace with an empty string and a \ escape. I've also tried """ or ''' round the string but neither work as I don't know which type of quotation mark will be first. Any suggestions would be appreciated.
fileName = filePath.rsplit('/', 1)[1]
stream = os.popen('strings ' + filePath)
output = stream.readlines()
file = open(filePath + "/" + fileName + "_StringsResults.txt", "w+")
for o in output:
file.write(str(o))
results += str(o)
file.close()
dsn = 'postgresql://############localhost:########/test?sslmode=disable'
conn = psycopg2.connect(dsn)
with conn.cursor() as cur:
cur.execute(
"UPSERT INTO test (testID) VALUES ('%s')" % (results))
conn.commit()
yes that worked, thanks a million. For anyone else who's interested the solution was roughly:
query = """UPSERT INTO test (testID) VALUES (%s)"""
#Connection code etc.
with conn.cursor() as cur:
cur.execute(query, [results])
conn.commit()
The [] round the parameter was necessary to avoid a type error.

Python program, inserting txt file to sqlite3 database

Currently working on a program in Python that has to take data from a text file and input it into appropriate place in SQLite. I have created my database and the columns, now I am stuck on how I process the text data in and read it into the sqlite database.
Here are a couple lines from text file.
Kernel version: Windows 10 Enterprise, Multiprocessor Free
Product type: Professional
Product version: 6.3
Service pack: 0
Here is what I have so far,
import sqlite3
conn = sqlite3.connect('systeminfo.db')
c = conn.cursor()
def create_table():
c.execute("""CREATE TABLE IF NOT EXISTS system_information (
Machine_Name text,
Kernel_version text,
Product_type text,
product_version text,
Registered_organization text,
registered_owner text,
system_root text,
processors text,
physical_memory text
)""")
create_table1()
This creates my database and my table just how I want it, now I am stuck on taking the for example Kernel version from text file and putting the "Windows 10 Enterprise" into the database under the Kernel_Version Column.
UPDATE:
After using #zedfoxus tips, I was able to successfully get data, here is what I have, now how can I do the next lines more efficient? I am using elif, getting errors,
def insert_data(psinfo):
with open(psinfo) as f:
file_data = f.readlines()
for item in file_data:
if 'Kernel version' in item:
info = item.strip().split(':')
val = info[1].strip().split(',')
elif 'Product type' in item:
info = item.strip().split(':')
val = info[1].strip().split(',')
c.execute(
'INSERT INTO system_information (Kernel_version,Product_type ) values(?,?)',
(val[1].strip(),)
)
conn.commit()
Let's say you have a file called kernel.txt that contains
Kernel version: Windows 10 Enterprise, Multiprocessor Free
Product type: Professional
Product version: 6.3
Service pack: 0
Your python code would just have to read that text file and insert data into SQLite like so:
import sqlite3
conn = sqlite3.connect('systeminfo.db')
c = conn.cursor()
def create_table():
# same thing you had...just removing it for brevity
def insert_data(filename):
# read all the lines of the file
with open(filename) as f:
file_data = f.readlines()
# if Kernel version exists in the line, split the line by :
# take the 2nd item from the split and split it again by ,
# take the first item and pass it to the insert query
# don't forget to commit changes
for item in file_data:
if 'Kernel version' in item:
info = item.strip().split(':')
val = info[1].strip().split(',')
c.execute(
'insert into system_information (Kernel_version) values(?)',
(val[0].strip(),)
)
conn.commit()
create_table()
insert_data('kernel.txt')
You will have to change this code if you have multiple files containing such information, or if you have a single file containing multiple blocks of similar information. This code will get you started, though.
Update
I have separated the data parsing into its own function that I can call multiple times. Note how I have created 3 variables to store additional information like product type and version. The insert execution is happening outside of the loop. We are, basically, collecting all information we need and then inserting in one shot.
import sqlite3
conn = sqlite3.connect('systeminfo.db')
c = conn.cursor()
def create_table():
# same thing you had...just removing it for brevity
pass
def get_value(item):
info = item.strip().split(':')
val = info[1].strip().split(',')
return val[0].strip()
def insert_data(filename):
# read all the lines of the file
with open(filename) as f:
file_data = f.readlines()
# if Kernel version exists in the line, split the line by :
# take the 2nd item from the split and split it again by ,
# take the first item and pass it to the insert query
# don't forget to commit changes
kernel_version = ''
product_type = ''
product_version = ''
for item in file_data:
if 'Kernel version' in item:
kernel_version = get_value(item)
elif 'Product type' in item:
product_type = get_value(item)
elif 'Product version' in item:
product_version = get_value(item)
c.execute(
'''insert into system_information
(Kernel_version, Product_type, Product_version)
values(?, ?, ?)''',
(kernel_version, product_type, product_version,)
)
conn.commit()
create_table()
insert_data('kernel.txt')

psycopg2 vs sys.stdin.read()

I have small code like below :
#!/usr/bin/python
import psycopg2, sys
try:
conn = psycopg2.connect("dbname='smdr' user='bino'");
except:
print "I am unable to connect to the database"
cur = conn.cursor()
v_num = '1'
cur.execute("SELECT * from genctr WHERE code = %(num)s", dict(num=v_num))
rows = cur.fetchall()
print "\nShow me the databases:\n"
ctrnum =0
for row in rows:
print row[0]+","+row[1]
when i run it, i got
bino#erp:~/mydoc/openerp/smdr$ ./genctr.py
Show me the databases:
1,Bahamas
1,Barbados
1,Canada
1,Cayman Islands
1,United States
1,Virgin Islands U.S.
I try to replace "v_num = '1' " with "v_num = sys.stdin.read()"
#!/usr/bin/python
import psycopg2, sys
try:
conn = psycopg2.connect("dbname='smdr' user='bino'");
except:
print "I am unable to connect to the database"
cur = conn.cursor()
#v_num = '1'
v_num = sys.stdin.read()
cur.execute("SELECT * from genctr WHERE code = %(num)s", dict(num=v_num))
rows = cur.fetchall()
print "\nShow me the databases:\n"
ctrnum =0
for row in rows:
print row[0]+","+row[1]
But when I run it , I only got this :
bino#erp:~/mydoc/openerp/smdr$ echo 1 |./genctr.py
Show me the databases:
Kindly please give me your enlightment on how to fix it
Sincerely
-bino-
echo 1 is going to give "1\n" to your program (that is, "1" with a newline character afterward). sys.stdin.read() is going to return that exact string, and then psycopg2 is going to prepare the SQL statement as SELECT * from genctr WHERE code = '1\n'. The result is going to be no matching results, so the code inside the for loop will never execute, which is why you don't see any extra output.
Try doing either echo -n 1 to suppress the newline, or sys.stdin.read().strip() to remove any leading and trailing whitespace from the string. If the code field is an integer, it might be a good idea to cast the result of sys.stdin.read() to an int, too, like so:
int(sys.stdin.read().strip())

Resources