CSV import to database - python

I am getting the error 'sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 4, and there are 1 supplied.' The below code should be making a database and creating a table with the the titles listed below. Then take values from a csv. file and add it under the allotted headings. Any help would be would be appreciated!
import const
import sqlite3
SEP = ','
DATA_FILENAME = 'pokemon.csv'
con = sqlite3.connect('poki.db')
cur = con.cursor()
cur.execute('DROP TABLE IF EXISTS poki')
cur.execute( ' CREATE TABLE poki( pokemon TEXT, species_id INTEGER,'
' height REAL, weight REAL)' )
values = ('INSERT INTO poki VALUES (?, ?, ?, ?)')
for line in DATA_FILENAME:
list_of_values = line.strip().split(SEP)
cur.execute(values, list_of_values)
cur.close()
con.commit()
con.close()

Related

sqlite SELECT statement returns None

I have an issue returning a string from my database query.
First step was to create a database:
def create_database():
# database setup
try:
con = sqlite3.connect('db/mydb.db')
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS user (id INTEGER NOT NULL PRIMARY KEY, balance REAL NOT NULL, text TEXT NOT NULL)')
con.commit()
con.close()
except Error as e:
print('Failed to setup database.\n' + e)
exit(1)
def get_connection():
try:
con = sqlite3.connect('db/mydb.db')
return con
except:
print('Unable to connect to database. Please try again later.\n')
exit(1)
My second step was creating a user and add him with INSERT to my database:
def create_user(user_id : int):
balance = 0.0; # base unit = USD
text = create_text()
# connect to database
con = get_connection()
cur = con.cursor()
cur.execute('INSERT INTO user (id, balance, text) VALUES (?, ?, ?)', (user_id, balance, text))
database = cur.execute('SELECT * FROM user').fetchone()
print(database)
con.commit()
con.close()
def create_text():
# do some stuff which creates my text
# the text is something like 'LNURL...'
return text
This is how the result of my database query looks like:
(393120847059091456, 0.0, 'LNURL1DP68GURN8GHJ7URP09JX7MTPDCHXGEF0D3H82UNVWQHKZURF9AMRZTMVDE6HYMP0XGMQA9V7RT')
If I try to query this database for my text it returns nothing/None. My print(text) just produces an empty new line.
def get_text(user_id : int):
# connect to database
con = get_connection()
cur = con.cursor()
cur.execute('SELECT text FROM user WHERE id=?', (user_id,))
text = cur.fetchone()
con.commit()
con.close()
print(text)
return text
I think my sqlite database used 32bit int values by default. So forcing it to use 64 bit when creating the table fixed my issue:
cur.execute('CREATE TABLE IF NOT EXISTS user (id INT8 PRIMARY KEY NOT NULL, balance REAL NOT NULL, text TEXT NOT NULL')
Than I can return my result of the query with this: return text[0]

Trouble inserting JSON data into sqlite3 database

I created an sqlite3 database using python to store data as shown in the code below
import sqlite3
conn = sqlite3.connect('tweets_data.sqlite')
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS tweets')
cur.execute('''
CREATE TABLE tweets (
id INTEGER PRIMARY KEY, created_at TEXT, full_text TEXT,
favourite_count INTEGER, retweet_count INTEGER)
''')
With this table, i want to store data from a JSON file (parts of the code are screenshotted and attached as images), which i have loaded as seen below
import json
with open('tweets.json') as f:
data = json.load(f)
After that, i tried inserting the data into the table using a for loop to pull out all unique tweet id and its following information. The code below is what i tried doing
for records in data:
cur.execute('INSERT INTO tweets (id, created_at, full_text, favourite_count, retweet_count) VALUES (?, ?, ?, ?, ?)',
(records['id'], records['created_at'], records['full_text'], records['user']['favourites_count'], records['retweet_count']))
conn.commit()
print(cur.fetchall())
conn.close()
However when i did a print (cur.fetchall()), the output was only an empty list. Nothing was inserted into the table. Thank you if anybody is able to help!
json file page 1
json file page 2
cur.fetchall() returns the result of the last query, and INSERT yields no result. You need a SELECT-query first:
cur.execute('SELECT * FROM tweets')
rows = cut.fetchall()

Python - SQLITE db update entrys from csv file

I am trying to update existing sqlite db rows with rows from my csv file.
Not delet and insert, but update existing row (keeping id).
dqlite db has 4 columns and only 4th column is different (to update).
if its not possible to update only one column i can accept updating whole row but keeping its place in db.
db before update:
cfthostname,cftshortname,cftenv,cert_time
1904h.net,1904h,tst,DD/MM/RRRR
19053.net,19053,tst,26/03/2021
2210010315.net,2210010315,prd,DD/MM/RRRR
1809m.net,1809m,tst,26/03/2021
13jw.net,13jw,acc,DD/MM/RRRR
csv to update:
cfthostname,cftshortname,cftenv,cert_time
1904h.net,1904h,tst,13/05/2023
19053.net,19053,tst,23/07/2023
13jw.net,13jw,acc,14/06/2029
update code:
import sqlite3
import csv
conn = sqlite3.connect("C:\db.sqlite3")
cursor = conn.cursor()
[...]
with open('C:\\csv\\update.csv','rt') as fin:
dr = csv.DictReader(fin)
to_db = [(i['hostname'], i['shortname'], i['env'], i['cert_time']) for i in dr]
cursor.executemany("UPDATE itpassed_host SET hostname = ?, shortname = ?, env = ?, cert_time = ?", to_db)
conn.commit()
conn.close()
tried also with () on to_db but it gives same output on db
cursor.executemany("UPDATE itpassed_host SET hostname = ?, shortname = ?, env = ?, cert_time = ?", (to_db))
db after update:
cfthostname,cftshortname,cftenv,cert_time
13jw.net,13jw,acc,14/06/2029
13jw.net,13jw,acc,14/06/2029
13jw.net,13jw,acc,14/06/2029
13jw.net,13jw,acc,14/06/2029
13jw.net,13jw,acc,14/06/2029
how to update only rows from csv to update correctly in db?
Use the WHERE condition.
import sqlite3
import csv
conn = sqlite3.connect("db.sqlite3")
cursor = conn.cursor()
with open('update.csv','rt') as fin:
dr = csv.DictReader(fin)
to_db = [(i['cert_time'], i['cfthostname'], i['cftshortname'], i['cftenv'],) for i in dr]
cursor.executemany("UPDATE itpassed_host SET cert_time = ? WHERE cfthostname = ? AND cftshortname = ? AND cftenv = ?", to_db)
conn.commit()
conn.close()
Notice the order changed in the line with to_db=.

Insert Python string or dictionary into MySQL

I have a Python string (or potentially a Python dictionary) that I'd like to insert to MySql table.
My String is the following:
{'ticker': 'BTC', 'avail_supply': 16479075.0, 'prices': 2750.99, 'name': 'Bitcoin', '24hvol': 678995000.0}
I have the same kind of error if I want to insert the Dict format.
I really don't understand this kind of error (i.e. the '\' in-between the components of the string).
How can I deal with this error? Any why to properly insert a whole string to a particular TEXT cell in SQL?
Many thanks !!
Here is how to connect, make a table, and insert in the table.
import MySQLdb as mdb
import sys
#connect
con = mdb.connect('localhost', 'testuser', 'test623', 'testdb');
with con:
#need the cursor object so you can pass sql commands, also there is a dictionary cursor
cur = con.cursor()
#create example table
cur.execute("CREATE TABLE IF NOT EXISTS \
Writers(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25))")
#insert what you want
cur.execute("INSERT INTO Writers(Name) VALUES('Jack London')")
cur.execute("INSERT INTO Writers(Name) VALUES('Honore de Balzac')")
cur.execute("INSERT INTO Writers(Name) VALUES('Lion Feuchtwanger')")
cur.execute("INSERT INTO Writers(Name) VALUES('Emile Zola')")
cur.execute("INSERT INTO Writers(Name) VALUES('Truman Capote')")
Example above will make a table with 2 cols, one ID and one name
look here on an example on how to insert stuff from dictionary with keys and list as value to sql, basically you need place holders
sql = "INSERT INTO mytable (a,b,c) VALUES (%(qwe)s, %(asd)s, %(zxc)s);"
data = {'qwe':1, 'asd':2, 'zxc':None}
conn = MySQLdb.connect(**params)
cursor = conn.cursor()
cursor.execute(sql, data)
cursor.close()
conn.close()
or you can go with this as an example for a simple straight forward dict
placeholders = ', '.join(['%s'] * len(myDict))
columns = ', '.join(myDict.keys())
sql = "INSERT INTO %s ( %s ) VALUES ( %s )" % (table, columns, placeholders)
cursor.execute(sql, myDict.values())

How to insert data into mysql table using python(MySQLdb)?

cur1 = connection.cursor()
cur3 = connection.cursor()
cur3.execute("SELECT * FROM TABLE1")
connection.commit()
for i in range(0,totalRow-1):
row = cur3.fetchone()
if tempId.__contains__(row[0]):
cur1.execute("insert into summary (id, description, resolution) values (%s, %s, %s)",(row[0],row[1],tempResolution[tempId.index(row[0])]))
The above code is not giving any error but data is not inserting in the database.
instead of last line, try this:
tuple = row[0], row[1], tempResolution[tempId.index(row[0])]
cur1.executemany("insert into summary (id, description, resolution) values (?,?,?)", tuple)
if that doesn't work please expand your code explaining what are totalRow, tempId.__contains__ and types of row[0],row[1],tempResolution[tempId.index(row[0])]

Categories

Resources