I am new in this and this is my first question. I hope you guys will help.
If my question format is wrong, feel free to comment on that also.
The code is pretty simple. I have DB connection, 2 functions - one for printing and another for choosing how many SQL queries I want to execute and input for those queries.
Idea is to enter a number(INT) of SQL queries - for example, 2 and then in another line user must enter 2 SQL queries.
After that, call_table function will print out current table status/situation/data.
For example - user wants to print out into console table data (table have 2 columns, [name][college], varchar type)
Insert a number of SQL queries you want to execute: 1
Insert SQL statement:
select * from student
('ivan', 'ino')
('nena', 'fer')
('tomislav', 'ino')
('marko', 'fer')
('tomislav', 'ino')
('marko', 'fer')
When I try to insert some values into the same table nothing happens with the table, data is not entered.
The query is 100% correct since I tested it in workbench, also I've tried to create another table from this program and the query was executed normally and the table was created.
I receive no errors.
Code is below:
import pymysql
db = pymysql.connect(host='localhost', user='root', passwd='123456', database='test')
mycursor = db.cursor()
def call_table(data_print):
for i in data_print:
print(i)
def sql_inputs(cursor):
container = []
no = int(input("Insert a number of SQL queries you want to execute: "))
for i in range(no):
container = [input("Insert SQL statement: \n").upper()]
for y in container:
cursor.execute(y)
sql_inputs(mycursor)
call_table(mycursor)
What am I doing wrong?
I tried even more complicated SQL queries but insert into the table is not working.
Thank you
Everything is good with the code, you're just missing cursor.commit()
By default cursor commit is false in python for insert queries.
cursor.execute(y)
cursor.commit()
and if you're done with queries
db.close()
You should append the queries to the container variable
import pymysql
db = pymysql.connect(host='localhost', user='root', passwd='123456', database='test')
mycursor = db.cursor()
def call_table(data_print):
for i in data_print:
print(i)
def sql_inputs(cursor):
container = []
no = int(input("Insert a number of SQL queries you want to execute: "))
for i in range(no):
container.append(input("Insert SQL statement: \n").upper())
for y in container:
cursor.execute(y)
sql_inputs(mycursor)
call_table(mycursor)
At the end of the program, I've added db.commit(), and everything works fine now.
import pymysql
db = pymysql.connect(host='localhost', user='root', passwd='45fa6cb2',
database='ivan')
mycursor = db.cursor()
def call_table(data_print):
for i in data_print:
print(i)
def sql_inputs(cursor):
container = []
no = int(input("Insert a number of SQL queries you want to execute: "))
for i in range(no):
container.append(input("Insert SQL statement: \n").upper())
for y in container:
cursor.execute(y)
sql_inputs(mycursor)
db.commit()
call_table(mycursor)
Related
import sys
import mysql.connector
mydb = mysql.connector.connect(host='localhost', user='root', passwd='anohacker', database='csproj')
cursor = mydb.cursor(buffered=True)
nameb=input("enter your name: ")
bookbor=int(input("Enter book code to borrow: "))
def borrow(nameb,bookbor):
bquery="update inventory set name_of_borrower=%s where book_code=%s"
stock1="update inventory set in_stock=in_stock-1 where book_code=%s"
stock2="update inventory set borrowed=borrowed+1 where book_code=%s"
cursor.execute(stock1,bookbor)
cursor.execute(stock2,bookbor)
cursor.execute(bquery,nameb,bookbor)
mydb.commit()
borrow([nameb],[bookbor])
I want to take name and book code from user and update my mysql table columns with them. But it's giving me an error. Most answers are for insert into but I want to update table.
mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement
you need to provide the data for the query as a tuple so:
bquery="update inventory set name_of_borrower=%s where book_code=%s"
cursor.execute(bquery,(nameb,bookbor))
see https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html
I'm using cx_oracle to update record data in oracle from python. It just a simple update, but it takes forever to run and timeout in the end. If I run the same statement directly from Oracle, it works perfectly. Does anyone know why this happened? Thanks!
my code:
con = cx_Oracle.connect()
cur = con.cursor()
stmt = "UPDATE table SET rank = 4 WHERE id like 'SAP_1000141471' and rank = 2"
cur.execute(stmt)
con.commit()
result =cur.fetchall()
I tried inserting the values into the DB through python. However i do not get any error but i do not see it updating in DB. Please advice.
#!/usr/bin/python
import MySQLdb
val = MySQLdb.connect(host='localhost', user='root', passwd='root123',
db='expenses')
def access_db(val):
access = val.cursor()
sql = """Insert into monthly values (2,'Food',1000)"""
access.execute(sql)
val.commit()
val.close()
Output from DB after the script execution:
MariaDB[expenses]> select * from monthly;
SL_no Type Amount
1 Fuel 500
I do not find the second entry in Db.
I dont think you are calling the access_db() function anywhere
I am using Python 2.7 and pyodbc to talk to a Postgresql database. The execute statement freezes and does not return when I use prepared statement. If I use straight sql then it works fine. Any ideas?
Here is what I have in code:
DBconn = pyodbc.connect("DSN=devDB;UID=dev;PWD=dev", autocommit=True)
cursor = DBconn.cursor()
sql = """ select distinct age from user where name = (?) """
params = ('john',)
cursor.execute(sql, params)
#works fine
#sql = """ select distinct age from user where name = 'john' """
#cursor.execute(sql)
row = cursor.fetchone()
print(row)
cursor.close()
DBconn.close()
PS: This query should only return one row and there is data is really small.
I have a python SQL query which is intended to call a procedure and then return that query in results however with the below code I only get this output:
adodbapi.adodbapi._SQLrows object at 0x0000000004CDE278
instead of it selecting the rows and data that it should be grabbing, I don't know anything about SQL and limited python so be gentle :)
# Connect to the SQL DB
conn = adodbapi.connect("Provider=SQLOLEDB; SERVER=xx.x.x.x; Initial Catalog=master_db;User Id=User; Password=Pass; ")
curs = conn.cursor()
# Execute SQL procedure "
curs.execute('util.referencing_procedure', )
results = curs.fetchall()
print results
conn.close()
You need to iterate through the results. Probably:
for row in results:
print row