Using SQLite3 with python - python

I just started learning SQLite3 for python today, and I'm having trouble figuring out why this won't work.
import sqlite3, os
if not os.path.isfile("G:\\Python\\My first database.db"):
dtabse = sqlite3.connect("G:\\Python\\My first database.db")
cursr = dtabse.cursor()
cursr.execute("""CREATE TABLE Students
(first_name text,
surname text,
DOB text,
Form text)
""")
cursr.execute(""" INSERT INTO Students
VALUES ("Dave", "Edwards", "16", "11AB")""")
dtabse.commit()
dtabse.close()
else:
dtabse = sqlite3.connect("G:\\Python\\My first database.db")
cursr = dtabse.cursor()
print(cursr.fetchall())
In a powerpoint I was viewing, it said fetchall() should retrieve everything and display it. On the first go of this program it won't find a file in this directory, so the if area gets executed. When I next run the program the else area gets executed.
That much works, on the first go the program ends and begins. On the second go it prints an empty list, when I was expecting the table. I checked the database file and the data is there, so why can't I print it?

You need a SELECT statement to retrieve the data you want.

Related

SqLite3: inserting text into text column gives binary data with Python

Hello and thank you for clicking on this question. I want to insert into the database the contents from a text file encoded with utf-8. When proceeding to insert the text content into the DB it tells me that it is binary data for some reason. When I created the DB in sqlite3, I have specified that the description (the column in question) to be TEXT, therefore I do not know what could be the problem...
The code is the following [it only contains the part where I insert into the DB]:
(quick summary of the code: it is looking into a folder with many text files and then it collects, some variables from the text's name and contents and then if the text has not been added to the DB then add a new row that contains the missing variables corresponding to the text file )
def put_inside_db():
counter = 0
for item in list_txt:
item_components = item.split("-")
item_year = item_components[-1].split(".")
unique_key = str(item_components[0]) + str(item_year[0])
cik = item_components[0]
comp_name = item_components[1]
year = item_year[0]
file_path = path_to_10k + item
file = open(file_path, "r+", encoding="utf-8")
description = file.read()
description = str(description)
print(description)
file.close()
if unique_key not in keys_db:
c.execute("INSERT INTO finaldata (cik, comp_name, year, unique_key, description) "
"VALUES(?,?,?,?,?)", (cik, comp_name, year, unique_key, description))
print("This key is not inside: " + unique_key)
counter += 1
else:
"do nothing"
# print("This key is inside: " + unique_key)
if counter % 50 == 0:
conn.commit()
conn.commit()
I even printed to the console the insides of the text files and they are strings, therefore I do not know why this issue is present. Below you can see the message that the DB displays when I click on the a value from the column "description"
UPDATE
I tried implementing the solution from the other question answered Forcing a data type (BLOB or TEXT) when inserting values into an SQLite table. Meaning I have done the following:
1) Tried fixing the values in the database by rewriting them as per the solution number one, but that did not fixed my problem
2) Another suggestion from the other post was that I should make sure that I insert text values into the DB. To my knowledge, the values that I try to insert into the DB are strings. To make sure I even forced the extracted description from the text files to be a string. However, that does not fix my problem..
Therefore, in my opinion I think that my question is not a duplicate since I insert strings into a column with affinity to text and it stores it as binary. If I am wrong about this, can someone please explain in a more detailed manner what is exactly happening and why I am getting this result. I have used similar code for other database insertions, but I have never received such an error...
Thank you!
Thanks to
https://stackoverflow.com/users/570339/ramy-al-zuhouri
and
sqlite for swift is unstable
sqlite3_bind_int(statement, 1, Int32(id))
sqlite3_bind_text(statement, 2,(type as NSString).UTF8String, -1, nil)
let desc = (description ?? "") as NSString
sqlite3_bind_text(statement, 3, desc.UTF8String, -1, nil)

How to iterate MySQL fetchall() results into tinter listbox and center results

just to explain whats going here: I have a search function that runs a MySQL query using user input [givenLocation]. It is supposed to dump the contents of the query into the listbox [self.lookuplist]. My issue is that currently it will only dump the first result even though I am using the fetchall() function. I am a self-taught python developer but I have not been able to find any information on this from other sources. Here is my code:
def searchL_button(self):
i = 0
givenLocation = self.top3.searchEntry1.get()
searchLookup = ("SELECT Status, Serial, Product_Code, Location FROM Registers WHERE Location = %s")
cursor9.execute(searchLookup, [givenLocation])
locRes = cursor9.fetchall() [i]
for i in locRes:
self.lookupList.insert(END, locRes)
You are setting the variable locRes to only contain the first result of your query. Change the last few lines to the following
locRes = cursor9.fetchall()
for curRes in locRes:
self.lookupList.insert(END, curRes)

Update statement for sqlite in python not working

I have made a simple for loop program which takes info from the user and updates the Database in sqlite. However my program does not update the DB, it just plainly ignores it. I have tried solutions from all over the net and haven't found anything just yet. I've given just the relevant stuff everything else works just fine.
query ="Alpha"
string = "Beta"
CreateDB = sqlite3.connect('check.db')
querycurs = CreateDB.cursor()
def createTable():
querycurs.execute('''CREATE TABLE Data1
(id INTEGER PRIMARY KEY, q TEXT, info TEXT)''')
createTable()
def addCust(q,info):
querycurs.execute('''INSERT INTO Data1 (q,info)
VALUES (?,?)''',(q,info))
addCust(query,string)
for i in range (1, 5):
string = input('What would you like to enter?: ')
querycurs.execute('UPDATE Data1 SET info =? WHERE q =?',(string,query))
CreateDB.commit()
If you get any syntax errors running this program its fine cause i edited the program so i could put it online. It's only the UPDATE statement you'd need to worry about. Cheerio.
Works fine for me. I suggest you change input() to raw_input() to enforce string data.

Are there any specific methods that my python program would continue from the killed line?

I have a working program that does some transforms but i'm quite afraid that, what will happen if the database it too bigger.
I'll make you clear like, if the below program bombs in the middle how would i get the program recover o get it working from the specified line of code.
Defenetely the process will get killed after executing a piece of code.
Will the program be able to continue back from where it got an error, or from the position were it got killed.
import sqlite3 as sqlite
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('removespecial.ini')
con = sqlite.connect('listofcomp.sqlite')
cur = con.cursor()
def transremovechars():
char_cfg = open('specialchars.txt', 'r') #Reads all the special chars to be removed from specialchars.txt#
special_chars = char_cfg.readline()
char_cfg.close()
cur.execute('select * from originallist')
for row in cur: #Applies transformation to remove chars for each row in a loop#
company = row[0]
for bad_char in special_chars:
company = company.replace(bad_char, '')
cur.execute('Create table transform1 (Names Varchar, Transformtype Varchar')
cur.execute('Insert into transform1 (Names)', company)
def transtolower():
cur.execute('select * from transform1') #Transformation to convert all the namesto lower cases#
for row in cur:
company = row[0]
company = company.lower()
cur.execute('Create table transform2 (Names Varchar, Transformtype Varchar') #Creates another table named transform2#
cur.execute('Insert into transform2 (Names)', company) #Copies all the lower cased names to transform2#
if __name__=="__main__":
transremovechars()
transtolower()
if the below program bombs in the middle how would i get the program recover o get it working from the specified line of code
You can't.
Your code is utterly mysterious because the create table will get an error prior to every insert except the first one.
If, however, you want to do a long series of inserts from one old table into one new table,
and you're worried about the possibility that it doesn't finish correctly, you have two choices for maintaining the required state information.
Unique Keys.
Batches.
Query.
Unique Keys.
If each row has a unique key, then some inserts will get an error because the row is a duplicate.
If the program "bombs", you just restart. You get a lot of duplicates (which you expected). This is not inefficient.
Batches.
Another technique we use is to query all the rows from the old table, and include a "batch" number that increments every 1000 rows. batch_number = row_count // 1000.
You create a "batch number" file with the number -1.
Your program starts, it reads the batch number. That's the last batch that was finished.
You read the source data until you get to the a batch number > the last one that finished.
You then do all the inserts from a batch.
When the batch number changes, do a commit, and write the batch number to a file. That way you can restart on any batch.
When restarting after a "bomb", you may get some duplicates from the partial batch (which you expected). This is not inefficient.
Query.
You can query before each insert to see if the row exists. This is inefficient.
If you don't have a unique key, then you must do a complex query to see if the row was created by a previous run of the program.

lastrowid() alternative or syntax without using execute in sqlite python?

In sqlite3 in python, I'm trying to make a program where the new row in the table to be written will be inserted next, needs to be printed out. But I just read the documentation here that an INSERT should be used in execute() statement. Problem is that the program I'm making asks the user for his/her information and the primary key ID will be assigned for the member as his/her ID number must be displayed. So in other words, the execute("INSERT") statement must not be executed first as the ID Keys would be wrong for the assignment of the member.
I first thought that lastrowid can be run without using execute("INSERT") but I noticed that it always gave me the value "None". Then I read the documentation in sqlite3 in python and googled alternatives to solve this problem.
I've read through google somewhere that SELECT last_insert_rowid() can be used but would it be alright to ask what is the syntax of it in python? I've tried coding it like this
NextID = con.execute("select last_insert_rowid()")
But it just gave me an cursor object output ""
I've also been thinking of just making another table where there will always only be one value. It will get the value of lastrowid of the main table whenever there is a new input of data in the main table. The value it gets will then be inserted and overwritten in another table so that every time there is a new set of data needs to be input in the main table and the next row ID is needed, it will just access the table with that one value.
Or is there an alternative and easier way of doing this?
Any help is very much appreciated bows deeply
You could guess the next ID if you would query your table before asking the user for his/her information with
SELECT MAX(ID) + 1 as NewID FROM DesiredTable.
Before inserting the new data (including the new ID), start a transaction,
only rollback if the insert failes (because another process was faster with the same operation) and ask your user again. If eveything is OK just do a commit.
Thanks for the answers and suggestions posted everyone but I ended up doing something like this:
#only to get the value of NextID to display
TempNick = "ThisIsADummyNickToBeDeleted"
cur.execute("insert into Members (Nick) values (?)", (TempNick, ))
NextID = cur.lastrowid
cur.execute("delete from Members where ID = ?", (NextID, ))
So basically, in order to get the lastrowid, I ended up inserting a Dummy data then after getting the value of the lastrowid, the dummy data will be deleted.
lastrowid
This read-only attribute provides the rowid of the last modified row. It is only set if you issued an INSERT statement using the execute() method. For operations other than INSERT or when executemany() is called, lastrowid is set to None.
from https://docs.python.org/2/library/sqlite3.html

Categories