Insert Data in mysql using python - 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()

Related

drop all databases at the same time

I want to drop all of my databases at the same time and I have done this function so far but unfortunately, it doesn't work and gives me no error at all nothing.
import mysql.connector
def drop_database():
print('CONNECTING ...')
mydb = mysql.connector.connect(
host="xxx",
user="xxx",
password="xxx",
port='xxx'
)
print('CONNECTED')
# database_list = []
drop_command = 'drop database'
show_databases = 'SHOW DATABASES;'
my_cursor = mydb.cursor(buffered=True)
my_cursor.execute(show_databases)
for show_databases in my_cursor:
my_cursor.execute(drop_command, str(show_databases[0]), ';')
for show_databases in my_cursor:
print(show_databases[0])
mydb.commit()
mydb.close()
drop_database()

Python array to mysql DB

Why i can't save a array list to mysql DB ^^.
This Script work
Code work:
########################################
# Importing modules
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="*******",
password="*********",
database="meineTestDB",
)
cursor = conn.cursor()
insert_stmt = (
"INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME)"
"VALUES (%s, %s)"
)
data = ('Test1', 'Test2')
try:
# Executing the SQL command
cursor.execute(insert_stmt, data)
# Commit your changes in the database
conn.commit()
except:
# Rolling back in case of error
conn.rollback()
print("Data inserted")
#Closing the connection
conn.close()
########################################
but i want save array to MySQL DB
I try | data = (cars1, cars2) | or | data = ((cars1), (cars2)) | but it doesn't work.
########################################
# Importing modules
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="*******",
password="*********",
database="meineTestDB",
)
cars1 = ["Ford", "Volvo", "BMW"]
cars2 = ["Ford", "Volvo", "BMW"]
cursor = conn.cursor()
insert_stmt = (
"INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME)"
"VALUES (%s, %s)"
)
data = (cars1, cars2)
try:
# Executing the SQL command
cursor.execute(insert_stmt, data)
# Commit your changes in the database
conn.commit()
except:
# Rolling back in case of error
conn.rollback()
print("Data inserted")
#Closing the connection
conn.close()
########################################
sry for this Symple question i'm new to that Space.
I hope you can help my.
THX for all answer.
It looks like your post is mostly code; please add some more details. 0.o

python Insert tupule value to mysql database

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)

How to have python function input() within mysql command?

Here is what i am trying to do:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="J"
)
mycursor = mydb.cursor()
sql = "UPDATE account SET balance = '100' WHERE address = 'input()'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
I want to know how to put input within the SQL command? Any other ways than that? I know in php that you put it within , but in python?
And what this code is all about is to refill or decrease customer balance.
like in db i got accounts and ofcourse there are alot of customers. How to make it so that it changes according to my input? thanks for reading.
Use the placeholder (%s) mechanism:
sql = "UPDATE account SET balance = '100' WHERE address = '%s'"
value = input()
mycursor.execute(sql, (value,)) # note well, the second argument must be a tuple, even if one-element only
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="J"
)
mycursor = mydb.cursor()
custumValue=input("Enter your Address")
sql = "UPDATE account SET balance = '100' WHERE address = %s"
mycursor.execute(sql,(custumValue,))
mydb.commit()
print(mycursor.rowcount, "record(s) affected")

python3 mysql connector module throws exception - ValueError: Could not process parameters

I am trying to get past this error that is haunting me. I built a simple script to populate a single column database.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE IF NOT EXISTS data(pass_word VARCHAR(20))")
val = 'test'
sql = "INSERT INTO data(pass_word) VALUES '%s'"
mycursor.execute(sql, (val))
mydb.commit()
It creates the table no problem, so I know the connector is working. But it refuses to insert the val into pass_word.
It throws the following exception
Press ENTER or type command to continue
Traceback (most recent call last):
File "sql-try.py", line 19, in <module>
mycursor.execute(sql, (val))
File "/usr/local/lib/python3.6/dist-packages/mysql/connector/cursor_cext.py", line 248, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/usr/local/lib/python3.6/dist-packages/mysql/connector/connection_cext.py", line 538, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
I think the issue was that my table only had one column, so when I was passing the val, val itself needed to be a tuple. Changing it to (sql, (val,)) did not address the issue. When I created a table with more than one column, the problem went away.
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="mydatabase"
)
mycursor = mydb.cursor()
myname = 'jimmy'
password = 'grizwald'
mycursor.execute("CREATE TABLE IF NOT EXISTS data3(my_name VARCHAR(20), pass_word VARCHAR(20))")
val = (myname, password )
sql = "INSERT INTO data3(my_name, pass_word) VALUES (%s, %s)"
mycursor.execute(sql, val)
mydb.commit()

Categories