i am trying to send two SQL ( select and update) in one python file. but getting still error
cursor = cnx.cursor()
query = "select id, mail from `candidats`; UPDATE candidats SET statusmail=1 "
results = cursor.execute(query, multi=True)
for cur in results:
print('cursor:', cur)
print('result:', cur.fetchall())
cursor.close()
cnx.close()
getting this error:
mysql.connector.errors.InterfaceError: No result set to fetch from
Related
create a database or connect to one#
conn = sqlite3.connect('address_book.db')
# Create cursor#
c = conn.cursor()
# Insert Into Table#
SQL = "INSERT INTO address_book(;bookID,BookName,MemberID,MemberName,Issue,Return) VALUES (?,?,?,?,?,?)"
c.execute(SQL)
# Commit Changes#
conn.commit()
I am trying to write a function that allows me to insert stuff into my database but it brings up sqlite3.OperationalError: near ";": syntax error and i dont know why.
I am using pyodbc.
query = "GRANT SELECT ON ABC.PROJE TO [MYDOMAIN\USER_1];"
connection.autocommit = True
cursor = connection.cursor()
result = cursor.execute(query)
connection.commit()
cursor.close()
connection.close()
All I get is an exception:
No Results. Previous SQL was not a query.
Is this method wrong, or are there any other ways to do this operation?
Thanks,
Python unable to read from MySql database. Connectivity works but no results returned
import mysql.connector
cnx = mysql.connector.connect(user='user', password='pwd',host='<host>', port='<port>',database='mydb')
cursor = cnx.cursor()
print("Connection success")
query = ("SELECT * from users")
cursor.execute(query)
for (row) in cursor:
print(row)
print("Print complete")
cursor.close()
cnx.close()
I get below result
Connection success
Print complete
If I add below line
rows=cursor.fetchall()
I get below error
InterfaceError: No result set to fetch from.
I have checked from MySql workbench and I get results from above query.
EDIT: The question is not about fetchall, even without fetchall I don't see any results.
EDIT 2:
Few Observations:
If I try with incorrect password, I get Access Denied error.
If I try with incorrect table name (with correct password) I get
error that table doesn't exist.
If I add below line before the for loop
print(len(cursor.description))
I get below error
TypeError: object of type 'NoneType' has no len()
I would to remove all customers who's names begin with SCH from my database. When I execute the code below it runs without error, but does not remove the data from the database.
cur = db.cursor()
sql = "DELETE FROM customers where IMAGE_ID like 'SCH%'"
cur.execute(sql)
after delete you need commit
conn = cx_Oracle.connect(...)
cur = db.cursor()
sql = "DELETE FROM customers where IMAGE_ID like 'SCH%'"
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
I'm having problems returning auto-incremented ID columns from a MySQL database using MySQLdb python library.
I have something like:
sql = """INSERT INTO %s (%s) VALUES (\"%s\")""" %(tbl, colsf, valsf)
try:
cursor.execute(sql)
id = cursor.lastrowid
db.close()
except:
print "Failed to add to MySQL database: \n%s" %sql
print sys.exc_info()
db.close()
exit()
However the lastrowid command seems to be returning incorrect values. For instance, I've tried printing out various id columns from the MySQL command line which shows them to be empty, but the lastrowid value keeps increasing by 1 every time the python script is run. Any ideas?
Turned out that the values weren't being committed to the MySQL database properly, adding "db.commit()" command seems to solve the problem.
sql = """INSERT INTO %s (%s) VALUES (\"%s\")""" %(tbl, colsf, valsf)
try:
cursor.execute(sql)
id = cursor.lastrowid
cursor.close()
db.commit()
db.close()
except:
print "Failed to add to MySQL database: \n%s" %sql
print sys.exc_info()
db.close()
exit()