python Insert tupule value to mysql database - python

I need to install tupule value to database,but getting "Unknown column 'Mac' in 'field list'" error
Below is the code i used
import mysql.connector, csv, sys
conn = mysql.connector.connect(
host="localhost",
user="root",
passwd="root",
database="mydjangoapp",
port=3307,
)
cursor=conn.cursor()
t1=('Mac', 'Mohan')
sql="insert into books (title,isbn) values(%s,%s)" %t1
cursor.execute(sql)

Don't use % interpolation, use placeholders.
t1 = ('Mac', 'Mohan',)
cursor.execute("INSERT INTO books (title, isbn) VALUES (%s, %s)", t1)

Related

Insert Data in mysql using python

import mysql.connector
mydb = mysql.connector.connect(
host="10.0.72.17",
user="admin",
passwd="1qaz!QAZ",
database="test"
)
mycursor = mydb.cursor()
sql = "INSERT INTO biage(kompaniis_saxeli) VALUES (%s)"
val = ('bane')
mycursor.execute(sql, val)
mycursor = mydb.cursor()
mydb.commit()
This is my python code , and i create column
kompaniis_saxeli varchar(225)
but when i try to run this code there is error
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
The python driver needs at least a 2 dimensional list for values
So use:
import mysql.connector
mydb = mysql.connector.connect(
host="10.0.72.17",
user="admin",
passwd="1qaz!QAZ",
database="test"
)
mycursor = mydb.cursor()
sql = "INSERT INTO biage(kompaniis_saxeli) VALUES (%s)"
val = ('bane',)
mycursor.execute(sql, val)
mycursor = mydb.cursor()
mydb.commit()

Read a csv file in python and insert the result into a MYSQL database

I have a csv file with the number of people infected by covid19 per country and per day. I have created a MYSQL database, a table with all the columns that the CSV file has and now I need to insert the rows into the database
I need to have a python code to achieve the task
import pandas as pd
import mysql.connector
mydb = mysql.connector.connect(
host=
user="root",
passwd=
database="covid19"
)
#Inserting data into the database
mycursor = mydb.cursor()
dataframe = pd.read_csv("total_cases.csv")
print(dataframe)
for row in dataframe:
print(index)
mycursor.execute("INSERT INTO covid_per_day_per_country (date, World,
Afghanistan, Albania, Algeria, Andorra"))
mydb.commit()
cursor.close()
I think you have to do mydb.commit() all the insert into.
Something like this
import csv
import MySQLdb
mydb = mysql.connector.connect(
host=
user="root",
passwd=
database="covid19"
)
mycursor = mydb.cursor()
dataframe = csv.reader(file('total_cases.csv'))
print(dataframe)
for row in dataframe:
mycursor.execute('INSERT INTO covid_per_day_per_country (date, World, Afghanistan, Albania, Algeria, Andorra") VALUES("%s", "%s", "%s", "%s", "%s", "%s")', row)
mydb.commit()
cursor.close()
Please refer this: Load CSV data into MySQL in Python

Using Python to Import CSV to a MySQL table

I can not execute a Python file to import CSV file to MySQL table.
import csv
import mysql.connector
mydb = mysql.connector.connect(
host='localhost',
user='root',
passwd='',
database='ricoh_oms'
)
cursor = mydb.cursor()
csv_data = csv.reader(open("slv_internal.csv"))
for row in csv_data:
cursor.execute("INSERT INTO slv_internal (GID, FullName, CostCenter) VALUES (%s, %s, %s)")
mydb.commit()
cursor.close()
print("Done")
I am newbie and dont understand the meaning of marker %s in the error. Thansk for your help. The error is:
Error:
"mysql.connector.errors.ProgrammingError: 1064 (42000): You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '%s, %s, %s)' at
line 1"
you are trying to Execute Parameterized Query
Example:
insert_stmt = (
"INSERT INTO employees (emp_no, first_name, last_name, hire_date) "
"VALUES (%s, %s, %s, %s)"
)
data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
cursor.execute(insert_stmt, data)
in code you are missing parameters:
cursor.execute("INSERT INTO slv_internal (GID, FullName, CostCenter) VALUES (%s, %s, %s)", ("value1","value2","value3"))
some documentation:
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

: Unknown column in 'field list'

Was trying to set time and the other value to record table but showing: Unknown column 'time' in 'field list', I define time into VARCHAR(255) AND make the value specific to "e" because it is char type, can anyone here help me out with this, much appreciated
import mysql.connector
import datetime
d = datetime.datetime.now()
e = d.strftime("%Y-%m-%d %H:%M:%S")
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="asd619248636",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.excute=("CREATE TABLE record (temperature FLOAT(20) , humidity FLOAT(20), time VARCHAR(255))")
#mycursor.execute("SHOW TABLES")
#for x in mycursor:
#print(x)
sql = "INSERT INTO record (temperature,humidity,time) VALUES (%s, %s, %s)"
val = (3.2,6.5,"e")
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
I think time is a keyword in mysql according to this
So in order to use it as a column name you will need to enclose it with backticks:
create_table_sql = "CREATE TABLE record (temperature FLOAT(20) , humidity FLOAT(20), `time` VARCHAR(255))"
sql = "INSERT INTO record (temperature,humidity,`time`) VALUES (%s, %s, %s)"

Inserting an array into mysql with Python

I am building this array with 40k entries.
array = [(value1, value2, value3),(value1, value2, value3),(value1, value2, value3) .... ]
Is it possible to insert this into mysql in python something like:
cursor.execute('''INSERT IGNORE into %s VALUES *array here*''' % (table_name, array))
I am having trouble passing the array variable into mysql correctly. Any help appreciated.
Yes you can do it with executemany:
cursor.executemany('INSERT IGNORE into %s VALUES(%s, %s, %s)'%table_name, sql_data)
Note: you shouldn't use % to pass values to the database, instead you need to pass them in the second parameter of execute/executemany. i used % for the table name because the first parameter is the prepared query string.
This is Use for me try it.
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="test"
)
mycursor = mydb.cursor()
print("Insert Process... Please Wait...")
for r in range(1,tdrows+1):
a = []
for c in range(1,tdcols+1):
a.append(driver.find_element_by_xpath("//*[#id='DataTables_Table_0']/tbody/tr["+str(r)+"]/td["+str(c)+"]").text)
sql = "INSERT IGNORE into test_table(id,fname,lname) VALUES (%s, %s, %s)"
val = (a)
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount, "Record Inserted.")

Categories