How to export text file data to mysql database - python

I want to export the text file data into MySQL database
import MySQLdb
import re
conn = MySQLdb.connect (host = "127.0.0.1", user = "root", passwd = "123456")
mycursor =conn.cursor()
mycursor.execute("CREATE DATABASE IF NOT EXISTS EMP")
mycursor.execute("USE EMP")
mycursor.execute("CREATE TABLE IF NOT EXISTS emp_details (Id VARCHAR(255) , Firstname VARCHAR(255),Lastname VARCHAR(255),department VARCHAR(255),salary VARCHAR(255)) ")
f = open("new.txt", "rb")
print (f.next())
for x in f:
mycursor.execute("INSERT INTO emp_details VALUES (%s,%s,%s,%s,%s)",x)
conn.commit()
print(mycursor.rowcount, "record inserted.")
I am getting an error
query = query % args
TypeError: not all arguments converted during string formatting
this is what my text file data looks like

Split your line on comma as you must provide 5 arguments to that execute string. Also, you should explicitly name those columns:
mycursor.execute("INSERT INTO emp_details (Id, Firstname, Lastname, department, salary) VALUES (%s,%s,%s,%s,%s)", x.split(","))

Related

Trouble filling MySQL table using Python

I'm am trying to convert a database I had as a txt file into a MySQL database. I found this tutorial and tried to follow it, but every time I run I get the error:
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'Pdf' in 'field list'
Here is the code I wrote
import shutil
import os
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
database="Book"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE IF NOT EXISTS Books (name VARCHAR(255), address VARCHAR(255))")
if os.path.exists("/home/Me/Desktop/Books/INFO.txt"):
#Extract the data from the txt file
f= open("/home/Me/Desktop/Books/INFO.txt","r")
Lines = f.readlines()
Items=[]
for line in Lines:
line=line.replace("\n","")
Items_not_s=line.split(",")
Items_not_s=tuple(Items_not_s)
Items.append(Items_not_s)
#Extracted the data from the txt file
sql = "INSERT INTO Books (Name, Pdf, Image, DegradedImage, Tags, Extra) VALUES (%s, %s, %s, %s, %s, %s)"
val = Items
print(val[20])
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "was inserted.")
else:
print("no file")
An example for the values (val) I want to put in the table is:
[('geekychefcookbook', '/home/Me/Desktop/Books/Food/Quarto Cookbooks/geekychefcookbook/PDF/PDF.pdf', 'None', '/home/Me/Desktop/Books/Food/Quarto Cookbooks/geekychefcookbook/IMAGE/IMAGE_RE.jpg', 'None', 'None'),('wickedgoodburgers', '/home/piotr/Desktop/Books/Food/Quarto Cookbooks/wickedgoodburgers/PDF/PDF.pdf', 'None', '/home/piotr/Desktop/Books/Food/Quarto Cookbooks/wickedgoodburgers/IMAGE/IMAGE_RE.jpg', 'None', 'None')]
Thanks in advance!
The MYSQL table does not have columns named Pdf, Image, DegradedImage, Tags, Extra as you are trying to insert.
The only columns you have are name and address, as defined in this line:
mycursor.execute("CREATE TABLE IF NOT EXISTS Books (name VARCHAR(255), address VARCHAR(255))")
Add all the columns in this line (by separating them with commas after address VARCHAR(255) with their specific data types and then try to insert your rows from the text file.

Python MySQL Update Query

I am trying to update a SQL Table given a users input I have the following code. The user can choose to enter in/change the below fields which are defaulted to the values in the SQL table. However when I run the code I get the following error message
mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement
I have counted it many times and it seems like the %s match the passed parameters. Am I missing something?
user = User_name_body.get('1.0',END)
passw = Password_text.get('1.0',END)
first = First_name.get('1.0',END)
last = Last_name.get('1.0',END)
phone = Phone_number.get('1.0',END)
email = Email_address.get('1.0',END)
mycursor = mydb.cursor()
sql = "UPDATE t_users SET Email_address=%s, First_name=%s, Last_name=%s, Phone_Number=%s, Password=%s WHERE User_Name=%s VALUES(%s,%s,%s,%s,%s,%s)"
val = (email, first, last, phone, passw,user)
mycursor.execute(sql, val)
mydb.commit()
mydb.close()
UPDATE does not take VALUES, you should change your sql query line to look like this:
sql = "UPDATE t_users SET Email_address=%s, First_name=%s, Last_name=%s, Phone_Number=%s, Password=%s WHERE User_Name=%s"
Python throws an error because you are asking for 12 parameters and only providing 6.
Prepare your sql data like this:
sql = """ UPDATE t_users SET Email_address=%s, First_name=%s, Last_name=%s, Phone_Number=%s, Password=%s WHERE User_Name = %s """
val = (email, first, last, phone, passw, user)
mycursor.execute(sql, val)
or you can do it like this
sql = "UPDATE btms_users SET btms_users.user='%s', btms_users.secret='%s' , btms_users.first_name='%s', " \
"btms_users.second_name='%s', btms_users.email='%s', btms_users.mobile='%s' " \
"WHERE btms_users.id='%s'" % (user_name, user_secret, user_firstname, user_lastname,
user_email, user_phone, user_id)
mycursor.execute(sql)
and here is a full working example:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="test",
database="test"
)
mycursor = mydb.cursor()
sql = "UPDATE items SET name = %s WHERE id = %s"
val = ("Test", 1)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")

Python Json TypeError: not all arguments converted during string formatting

i'm new in json. i'm work with mysql and json in my python project. i have json file like this:
i want to store some contents to sql database, but i have an error TypeError: not all arguments converted during string formatting when i run the code. Here's the piece of code:
def create_db(db_name, table_name):
# try:
db = dbconnect()
cursor = db.cursor()
cursor.execute("SET sql_notes = 0;")
cursor.execute("CREATE DATABASE IF NOT EXISTS {}".format(db_name))
cursor.execute("SET sql_notes = 0;")
cursor.execute(
"""CREATE TABLE IF NOT EXISTS {}.{}(waktu varchar(150),plate varchar(20),region varchar(150), score varchar(20), filename varchar(50));""".format(db_name, table_name))
cursor.execute("SET sql_notes = 1;")
with open('data.json') as f:
data = json.load(f)
for i in data:
cursor.execute(
"""INSERT INTO {}.{}(waktu, plate, region, score, filename) VALUES(%s,%s)""".format
(db_name, table_name),
(i['timestamp'], i['results'][0]['plate'], i['results'][0]['region']['code'], i['results'][0]['score'], i['filename']))
db.commit()
db.close()
# except Exception as e:
# print(e)
create_db(db_name="plate_recognizer", table_name="kendaraan")
how to solve this? any helps will be appriciate, thankyou
The problem is here:
"""INSERT INTO {}.{}(waktu, plate, region, score, filename) VALUES(%s,%s)""".format
(db_name, table_name),
(i['timestamp'], i['results'][0]['plate'], i['results'][0]['region']['code'], i['results'][0]['score'], i['filename']))
You provided 5 arguments((i['timestamp'], i['results'][0]['plate'], i['results'][0]['region']['code'], i['results'][0]['score'], i['filename']))) but only has 2 formats(VALUES(%s,%s)). You should add more formats like this: VALUES(%s, %s, %s, %s, %s)(use proper format if needed).

Failed to insert user input into MySQL database

I have created a database and inserted some value using python manual code but when i tried to taking input from user then inserting that input to my database table,i failed as i tried many ways.
Here is my code
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="Adee11ruchi#",
database="hdatabase"
)
mycursor = mydb.cursor()
name= str(input("What is your first name? "))
address=str(input("enter address:"))
#mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
mycursor.execute = ("""INSERT INTO customers (name, address) VALUES (r{}, r{})""".format(name, address))
#val = ('Peter', 'Lowstreet 4')
mydb.commit()
print(mycursor.rowcount, "record inserted.")
It showing me as
What is your first name? diyu
enter address:hiouy
-1 record inserted.
What is the issue,i failed to find out.
You should be using a prepared statement here. Consider this version:
mycursor = mydb.cursor(prepared=True)
name = input("What is your first name? ")
address = input("enter address:")
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
mycursor.execute = (sql, (name, address,))
mydb.commit()
The main takeaways points here are that you leave the values to be bound as parameters %s, and then you bind the values as a tuple in the call to cursor#execute. Note that the prepared statement API will handle the proper formatting of the inputs for you.

how do i dump a single sqlite3 table in python?

I would like to dump only one table but by the looks of it, there is no parameter for this.
I found this example of the dump but it is for all the tables in the DB:
# Convert file existing_db.db to SQL dump file dump.sql
import sqlite3, os
con = sqlite3.connect('existing_db.db')
with open('dump.sql', 'w') as f:
for line in con.iterdump():
f.write('%s\n' % line)
You can copy only the single table in an in memory db:
import sqlite3
def getTableDump(db_file, table_to_dump):
conn = sqlite3.connect(':memory:')
cu = conn.cursor()
cu.execute("attach database '" + db_file + "' as attached_db")
cu.execute("select sql from attached_db.sqlite_master "
"where type='table' and name='" + table_to_dump + "'")
sql_create_table = cu.fetchone()[0]
cu.execute(sql_create_table);
cu.execute("insert into " + table_to_dump +
" select * from attached_db." + table_to_dump)
conn.commit()
cu.execute("detach database attached_db")
return "\n".join(conn.iterdump())
TABLE_TO_DUMP = 'table_to_dump'
DB_FILE = 'db_file'
print getTableDump(DB_FILE, TABLE_TO_DUMP)
Pro:
Simplicity and reliability: you don't have to re-write any library method, and you are more assured that the code is compatible with future versions of the sqlite3 module.
Con:
You need to load the whole table in memory, which may or may not be a big deal depending on how big the table is, and how much memory is available.
Dump realization lies here http://coverage.livinglogic.de/Lib/sqlite3/dump.py.html (local path: PythonPath/Lib/sqlite3/dump.py)
You can modify it a little:
# Mimic the sqlite3 console shell's .dump command
# Author: Paul Kippes <kippesp#gmail.com>
def _iterdump(connection, table_name):
"""
Returns an iterator to the dump of the database in an SQL text format.
Used to produce an SQL dump of the database. Useful to save an in-memory
database for later restoration. This function should not be called
directly but instead called from the Connection method, iterdump().
"""
cu = connection.cursor()
table_name = table_name
yield('BEGIN TRANSACTION;')
# sqlite_master table contains the SQL CREATE statements for the database.
q = """
SELECT name, type, sql
FROM sqlite_master
WHERE sql NOT NULL AND
type == 'table' AND
name == :table_name
"""
schema_res = cu.execute(q, {'table_name': table_name})
for table_name, type, sql in schema_res.fetchall():
if table_name == 'sqlite_sequence':
yield('DELETE FROM sqlite_sequence;')
elif table_name == 'sqlite_stat1':
yield('ANALYZE sqlite_master;')
elif table_name.startswith('sqlite_'):
continue
else:
yield('%s;' % sql)
# Build the insert statement for each row of the current table
res = cu.execute("PRAGMA table_info('%s')" % table_name)
column_names = [str(table_info[1]) for table_info in res.fetchall()]
q = "SELECT 'INSERT INTO \"%(tbl_name)s\" VALUES("
q += ",".join(["'||quote(" + col + ")||'" for col in column_names])
q += ")' FROM '%(tbl_name)s'"
query_res = cu.execute(q % {'tbl_name': table_name})
for row in query_res:
yield("%s;" % row[0])
# Now when the type is 'index', 'trigger', or 'view'
#q = """
# SELECT name, type, sql
# FROM sqlite_master
# WHERE sql NOT NULL AND
# type IN ('index', 'trigger', 'view')
# """
#schema_res = cu.execute(q)
#for name, type, sql in schema_res.fetchall():
# yield('%s;' % sql)
yield('COMMIT;')
Now it accepts table name as second argument.
You can use it like this:
with open('dump.sql', 'w') as f:
for line in _iterdump(con, 'GTS_vehicle'):
f.write('%s\n' % line)
Will get something like:
BEGIN TRANSACTION;
CREATE TABLE "GTS_vehicle" ("id" integer NOT NULL PRIMARY KEY, "name" varchar(20) NOT NULL, "company_id" integer NULL, "license_plate" varchar(20) NULL, "icon" varchar(100) NOT NULL DEFAULT 'baseicon.png', "car_brand" varchar(30) NULL, "content_type_id" integer NULL, "modemID" varchar(100) NULL, "distance" integer NULL, "max_speed" integer NULL DEFAULT 100, "max_rpm" integer NULL DEFAULT 4000, "fuel_tank_volume" integer NULL DEFAULT 70, "max_battery_voltage" integer NULL, "creation_date" datetime NOT NULL, "last_RFID" text NULL);
INSERT INTO "GTS_vehicle" VALUES(1,'lan1_op1_car1',1,'03115','baseicon.png','UFP',16,'lan_op1_car1',NULL,100,4000,70,12,'2011-06-23 11:54:32.395000',NULL);
INSERT INTO "GTS_vehicle" VALUES(2,'lang_op1_car2',1,'03','baseicon.png','ыва',16,'lan_op1_car2',NULL,100,4000,70,12,'2011-06-23 11:55:02.372000',NULL);
INSERT INTO "GTS_vehicle" VALUES(3,'lang_sup_car1',1,'0000','baseicon.png','Fiat',16,'lan_sup_car1',NULL,100,4000,70,12,'2011-06-23 12:32:09.017000',NULL);
INSERT INTO "GTS_vehicle" VALUES(4,'lang_sup_car2',1,'123','baseicon.png','ЗАЗ',16,'lan_sup_car2',NULL,100,4000,70,12,'2011-06-23 12:31:38.108000',NULL);
INSERT INTO "GTS_vehicle" VALUES(9,'lang_op2_car1',1,'','baseicon.png','',16,'1233211234',NULL,100,4000,70,12,'2011-07-05 13:32:09.865000',NULL);
INSERT INTO "GTS_vehicle" VALUES(11,'Big RIder',1,'','baseicon.png','0311523',16,'111',NULL,100,4000,70,20,'2011-07-07 12:12:40.358000',NULL);
COMMIT;
By iterdump(), all information would be displayed like this:
INSERT INTO "name" VALUES(1, 'John')
INSERT INTO "name" VALUES(2, 'Jane')
INSERT INTO "phone" VALUES(1, '111000')
INSERT INTO "phone" VALUES(2, '111001')
An easy way is by filter certain keywords by string.startswith() method.
For example, the table name is 'phone':
# Convert file existing_db.db to SQL dump file dump.sql
import sqlite3, os
con = sqlite3.connect('existing_db.db')
with open('dump.sql', 'w') as f:
for line in con.iterdump():
if line.startswith('INSERT INTO "phone"'):
f.write('%s\n' % line)
Not very smart, but can fit your objective.

Categories

Resources