SQLite3 help, saving variables into a databse - python

I have 2 things I needed help with:
1) I am unsure as to how I can check if a table exists in python using the sqlite3 library.
2) I am unsure as to how I can save variables from the program to a database. I want to be able to check if UserDetails exists before making the database.
I've been reading around and everyone is doing stuff differently,
Here is the section of my code that is responsible for saving the variables:
connection = sqlite3.connect("UserDetails.db")
crsr = connection.cursor()
#create table
sql_command = table_creator
crsr.execute(sql_command)
#insert values into table
data_to_insert = (username, first_name, surname, age, user_salt, password_hash, date_today)
sql_command = """INSERT INTO UserDetails VALUES ((?, ?, ?, ?, ?, ?, ?), data_to_insert);"""
crsr.execute(sql_command)
connection.commit() #save changes
connection.close() #terminate connection
and in case you want to see table_creator it looks like this:
table_creator = '''CREATE TABLE `UserDetails` (
`Username` VARCHAR(8) NOT NULL,
`Firstname` VARCHAR(10) NOT NULL,
`Surname` VARCHAR(20) NOT NULL,
`Age` INT(2) NOT NULL,
`Salt` VARCHAR(10) NOT NULL,
`Hash` VARCHAR(64) NOT NULL,
`Date` DATE NOT NULL,
PRIMARY KEY (`UserName`)
);'''
I will appreciate and feedback or support.
I am still learning to code, and my CompSci teacher doesnt teach us Python specifically, so what I know is self taught.
Oh and this is the error message I get:
Traceback (most recent call)
File "c:/Users/Arslan/Project A2/login.py", line 99, in <module>
save_details()
File "c:/Users/Arslan/Project A2/login.py", line 93, in save_details
crsr.execute(sql_command)
sqlite3.OperationalError: no such column: data_to_insert

How to check if a table exists or no :
The first way :
Use this query:
SELECT name FROM sqlite_master WHERE type='table' AND name='{table_name}';
Modify {table_name} with your table to check
There are two cases :
. If the cursor equal to 0 ==> the table does not exist
Else, the table exists
The second way:
Use :
PRAGMA table_info(table_name)
example:
The third way :
Use this query :
select 1 from table
It will return the constant 1 for every row of the table if the table exists, or nothing if not.
There are many other ways, but I listed the best in my opinion.
How to save variables from the program to a database:
To insert data into sqlite3, you can use :
cursor.execute("insert into UserDetails values (?, ?, ?, ?, ?, ?, ?)", (username, firstname, surname, age, salt, hash, date))
DON'T USE (SQL injection):
cursor.execute("insert into UserDetails values ('{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')".format(username, firstname, surname, age, salt, hash, date))
Don't forget :
conn.commit()
Or you can use instead of it the connection as a context manager:
with conn:
# then cursor.execute..

1) I am unsure as to how I can check if a table exists in python using the sqlite3 library.
Use CREATE TABLE IF NOT EXISTS:
table_creator = '''CREATE TABLE IF NOT EXISTS `UserDetails` (
`Username` VARCHAR(8) NOT NULL,
`Firstname` VARCHAR(10) NOT NULL,
...
);'''
2) I am unsure as to how I can save variables from the program to a database.
You can pass variables for insert with the following syntax:
data_to_insert = (username, first_name, surname, age, user_salt, password_hash, date_today)
sql_command = '''INSERT INTO UserDetails VALUES (?, ?, ?, ?, ?, ?, ?)''';
crsr.execute(sql_command, data_to_insert )

Related

SQLite dynamically insert multiple values

I have a python web scraper that scrapes an apartment website and compiles various pieces of information about each property. I'm trying to use SQLite to dynamically insert each variable from the scraper to the SQLite DB. So Far I have written this code and it successfully inserts 1 row. For this to work I need it to insert all the rows. Could this be solved with a for loop?
'''
conn = sqlite3.connect('RentScraper.db')
c = conn.cursor()
def create_table():
# Create table
c.execute("""CREATE TABLE IF NOT EXISTS property (\
property_name varchar (250) NOT NULL,\
street_address varchar (20) NOT NULL,\
rent_price varchar (20) NULL,\
available_units INT NULL,\
contact_number varchar(50) NULL,\
PRIMARY KEY (property_name))""")
def dynamic_data_entry():
c.execute("INSERT INTO property (property_name, street_address, rent_price, available_units, contact_number) VALUES (?, ?, ?, ?, ?)",
(name, city, price, units_avl, contact))
conn.commit()
create_table()
dynamic_data_entry()
'''

pyodbc insert query with where clause with variable values

I want to insert different variable values in SQl where the values must not be present in the table using pyodbc but gets error.
variables
for data in datafield:
id = data.Id
iname = data.name
ivalue = data.value
cursor.execute("INSERT INTO Description (SrId, FieldName, FieldValue) VALUES (?, ?, ?) WHERE IN (SELECT * from Description WHERE SrId <>id AND FieldName<>iname AND FieldValue<>ivalue)", (id, iname, ivalue))
connection.commit()
Using Pyodbc to connect the Sql server with python
Error : Syntax error
In SQL Server, there is no VALUES...WHERE syntax. Consider an insert-select with a LEFT JOIN...NULL on table value constructor to avoid duplicates:
# PREPARED STATEMENT WITH QMARK PLACEHOLDERS
sql = """INSERT INTO Description (SrId, FieldName, FieldValue)
SELECT vals.SrId, vals.FieldName, vals.FieldValue
FROM Description d
LEFT JOIN (VALUES (?, ?, ?)) AS vals(SrId, FieldName, FieldValue)
ON d.SrId = vals.SrId
AND d.FieldName = vals.FieldName
AND d.FieldValue = vals.FieldValue
WHERE d.SrId IS NULL
OR d.FieldName IS NULL
OR d.FielValue IS NULL
"""
# BIND PARAMS
cur.execute(sql, (id, iname, ivalue))
connection.commit()
You have an extra " at the end of the line.
cursor.execute("INSERT INTO Description (SrId, FieldName, FieldValue) VALUES (?, ?, ?) WHERE IN (SELECT * from Description WHERE SrId <>id AND FieldName<>iname AND FieldValue<>ivalue)", (id, iname, ivalue)")
Should be
cursor.execute("INSERT INTO Description (SrId, FieldName, FieldValue) VALUES (?, ?, ?) WHERE IN (SELECT * from Description WHERE SrId <>id AND FieldName<>iname AND FieldValue<>ivalue)", (id, iname, ivalue))

cursor.fetchone() returning None after attempting to insert?

I have a table in an SQLite3 Database that looks like this:
CREATE TABLE "user" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
"discriminator" TEXT NOT NULL,
"userid" INTEGER NOT NULL UNIQUE,
"username" TEXT NOT NULL,
"matches_played" INTEGER NOT NULL,
"match_wins" INTEGER NOT NULL,
"match_losses" INTEGER NOT NULL,
"match_draws" INTEGER NOT NULL,
"rating" REAL NOT NULL,
"plays_game" INTEGER,
FOREIGN KEY("plays_game") REFERENCES "ygo_games"("game_id")
);
And after attempting an insert like this:
arg = tuple(user_data)
statement = db_cursor.execute('INSERT INTO user VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, NULL)', arg)
result = db_cursor.fetchone()
With the arg tuple being like this:
arg = ('1234', 123123123123, 'Name', 0, 0, 0, 0, 1000)
The result ends up being 'None'. Why could this happen?
Edit: Just realized that INSERT statements don't return any rows, but still, what would be the best way to check if the execution was successful from Python?
Per docs, to check if query was successful use the cursor.lastrowid attribute:
If the INSERT or REPLACE statement failed to insert the previous successful rowid is returned.
Also, consider the recommended practice of explicitly specifying the append columns to be clear of mapping of variables from VALUES or SELECT clauses. This always allows you to omit the auto increment column (which by the way is not necessary in SQLite) and last column, plays_game:
sql = '''INSERT INTO user ("discriminator", "userid", "username", "matches_played",
"match_wins", "match_losses", "match_draws", "rating")
VALUES(?, ?, ?, ?, ?, ?, ?, ?)'''
statement = db_cursor.execute(sql, arg)
id = db_cursor.lastrowid
db_conn.commit() # IMPORTANT TO PERSIST CHANGES

SQLite3 Python 2.7 sqlite3.OperationalError syntax error

import sqlite3
import codecs
sqlite3.connect("test.db")
db.execute("""CREATE TABLE IF NOT EXISTS STATIONS
(StationUID INT PRIMARY KEY NOT NULL,
StationNumber TEXT NOT NULL,
StationName TEXT NOT NULL,
StationLongitude REAL NOT NULL,
StationAltitude REAL NOT NULL);""")
print "Created Table STATIONS successfully"
cr = db.cursor()
name = "b"
station_number = "0123"
longitude = 13.4
altitude = 34.4
cr.execute("INSERT INTO STATIONS VALUES", (None, station_number, name, longitude, altitude))
db.commit()
It throws sqlite3.OperationalError: near "VALUES": syntax error, but I don't understand why, because it is the same syntax I found in the example.
You need to specify what values to insert:
INSERT INTO STATIONS VALUES (value1, value2, ...)
You cannot omit that part. Use SQL parameters to map Python values to those locations in the SQL syntax:
cr.execute(
"INSERT INTO STATIONS VALUES (?, ?, ?, ?, ?)",
(None, station_number, name, longitude, altitude))
The INSERT command is not complete. You need to insert the values into the INSERT by writing:
cr.execute("INSERT INTO STATIONS VALUES (?, ?, ?, ?, ?)", (None, station_number, name, longitude, altitude))

Python : Inserting multiple values into a table from excel

I have to read data from Excel and insert it into Table...
For this I am using Python 2.7, pymssql and xlrd modules...
My sql connection is working fine and I am also able to read data from Excel properly.
My table structure :
CREATE TABLE MONTHLY_BUDGET
(
SEQUENCE INT IDENTITY,
TRANSACTION_DATE VARCHAR(100),
TRANSACTION_REMARKS VARCHAR(1000),
WITHDRAWL_AMOUNT VARCHAR(100),
DEPOSIT_AMOUNT VARCHAR(100),
BALANCE_AMOUNT VARCHAR(100)
)
My excel values are like this :
02/01/2015 To RD Ac no 147825000874 7,000.00 - 36,575.74
I am having problem while inserting multiple values in the table... I am not sure how to do this...
import xlrd
import pymssql
file_location = 'C:/Users/praveen/Downloads/OpTransactionHistory03-01-2015.xls'
#Connecting SQL Server
conn = pymssql.connect (host='host',user='user',password='pwd',database='Practice')
cur = conn.cursor()
# Open Workbook
workbook = xlrd.open_workbook(file_location)
# Open Worksheet
sheet = workbook.sheet_by_index(0)
for rows in range(13,sheet.nrows):
for cols in range(sheet.ncols):
cur.execute(
" INSERT INTO MONTHLY_BUDGET VALUES (%s, %s, %s, %s, %s)", <--- Not sure!!!
[(sheet.cell_value(rows,cols))])
conn.commit()
Error :
ValueError: 'params' arg () can be only a tuple or a dictionary.
The docs are here : http://pymssql.org/en/stable/pymssql_examples.html
The exception you are getting says that the "'params' arg() can be only a tuple or a dictionary" but you're passing in a list. Also, your parameter list appears to be a single tuple instead of a list with 4 values. Try changing
cur.execute(
" INSERT INTO MONTHLY_BUDGET VALUES (?, ?, ?, ?, ?)", <--- Not sure!!!
[(sheet.cell_value(rows,cols))])
to
cur.execute(
" INSERT INTO MONTHLY_BUDGET VALUES (?, ?, ?, ?, ?)", <--- Not sure!!!
(sheet.cell_value(rows,cols)))
... or maybe
cur.execute(
" INSERT INTO MONTHLY_BUDGET VALUES (?, ?, ?, ?, ?)", <--- Not sure!!!
((sheet.cell_value(rows,cols))))
NB: untested. I've always changed how the bind variables in your SQL are being called.

Categories