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()
Related
I want to make one SELECT query from python to mysql DB.
But I'm getting an empty list when I call cursor.fetchall()
I have already tested the connection and the query with DBeaver, and it works fine.
I tried following the tutorials on https://dev.mysql.com but didn work.
Here is my function:
import mysql.connector
from mysql.connector import connect
def execute_query_2(self,query):
connection = connect(
host="config.host",
database="config.db_name",
user="config.db_user",
password="config.db_user"
)
print(connection)
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
print(result)
for row in result:
print(row)
The print(connection) gives me mysql.connector.connection_cext.CMySQLConnection object at 0x7f6a725bfca0.
Also the query is being loaded successfully as 'SELECT * from occUserManager.addressInformation'.
The result for this should bring 44 rows.
Any help is more than welcome.
Im trying to run some simple test code on our SQL servers, but no results are returning and there isn't an error being thrown.
Ive tried having the connect() with all the information on one line and also having each line in '' but nothing has made a difference so far. I also tried to close the connection after at the end of test() and that also didnt work.
from pyodbc import *
conn = connect(
Driver='{SQL Server}',
Server='servername',
Database='databasename',
Trusted_connection='yes'
)
def test():
cursor = conn.cursor()
cursor.execute('SELECT TOP 100 * FROM TABLENAME')
cursor.fetchall()
for row in cursor:
print(row)
conn.close()
test()
Earlier i ran this code twice and revived proper results. Now i dont pull results nor is there an error code explaining why.
This code gives an error on conn.commit line.
Error: ('HY000', "[HY000] [MySQL][ODBC 3.51 Driver]Commands out of sync; you can't run this command now (2014) (SQLEndTran)")
Whenever I call SP the ID is keep increasing in table but record is not inserting.
#app.route("/insert_user")
def insert_user():
try:
conn = pyodbc.connect("DRIVER={/usr/local/lib/libmyodbc3.so};SERVER=localhost;DATABASE=user_data;USER=user;PASSWORD=user_pass;OPTION=3;autoCommit = True")
cur = conn.cursor()
cur.execute("{call insert_user_sp(0,'aby#gmail.com','345','male','1992-01-12','www.facebook.com','abc','xyz','p','jr','english','i am student')}")
conn.commit()
except Error as e:
print e
finally:
cur.close()
conn.close()
Since you're using MySQL on Linux, I would recommend using a MySQL Python package rather than pyodbc. I use pyodbc for connecting to Microsoft SQL Server, but this package when using MySQL:
https://pypi.python.org/pypi/mysqlclient
Then you can use cursor.callproc():
http://mysqlclient.readthedocs.org/en/latest/user_guide.html?highlight=callproc#cursor-objects
Good luck!
By using Python and cx_Oracle, I am trying to insert rows to a table.
con = cx_Oracle.connect(ORACLE_USER+'/'+PASS+'#'+TNS)
cursor = con.cursor()
...
try:
cursor.executemany("INSERT INTO table(ID,NAME) VALUES(...)"
except cx_Oracle,exc:
error ,=exc.args
print error.code
print error.message
cursor.close()
con.close()
After insert all the rows from an input file, by using select query in cx_Oracle, I can see the inserted rows. However, sqlplus gives no results when I enter "select * from table;"
Is there something that I missed about cx_Oracle or is there a buffer in oracle client that shows the old results with sqlplus when it is connected to a remote db?
Have you committed your insert?
con.commit() #after inserts
or
con.autocommit = true #before inserts
I had an inverted problem: I added rows using sqlquery and after 2 hours of suffering read this post and guess, that I should close my session. I closed the console and managed to get my data!
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