I installed the MySQL connector from Oracle, and entered the following commands in Python (currently running Python 2.7.6)
import mysql.connector
cnx=mysql.connector.connect(user='genome',host='genome-mysql.cse.ucsc.edu',database='hg19')
cursor=cnx.cursor()
query=('show tables')
cursor.execute(query)
Nothing happened! I expected to see a list of tables. Why?
Incidentally, I tried this as well, with the same result:
query=('SELECT * FROM wgRna')
cursor.execute(query)
I know I have MySQL properly installed on my computer, because when I enter the same commands into the terminal everything is fine. Can someone explain to me what I'm doing wrong in Python?
You never did anything with the selected data; print the rows by iterating over the cursor after executing a query:
query = 'show tables'
cursor.execute(query)
for row in cursor:
print row
Related
I am trying to truncate a table in my Postgres DB using a python script:
conn = get_psql_conn()
cursor = conn.cursor()
cursor.execute("""TRUNCATE table table_name;""")
cursor.close()
conn.close()
Nothing happens.
The script finishes quickly, no error is raised.
The table still has its rows.
I was able to execute other queries with no problem using the same setup.
I did appreciate it if anyone can point out my mistake here!
Thanks
so I was trying to create a password manager for myself, using python and mariadb. After creating a table named pw, which contains Name, Account and Passwords 3 columns, I tried to create a function(Search_Passwords(app_name)) which I can use to enter a keyword to search in the database, and it will give me the right passwords. However, I ran into this error message:
Commands out of syncs error message.
I'm new to python and mariadb(using it cause for some reason MySQL doesn't work..), tried to look up for some answers but still can't figure it out. Can anyone help please? Below are other codes I think might be related.
This is what mariadb's table looks like.
Search_Passwords()
class UseDataBase
This is what I found online as a reference version where Search_Passwords() involved.
Sorry if my codes are not perfect... :(
MariaDB Connector/Python by default use unbuffered result sets, which means before executing another cursor all pending result sets need to be fetched or the cursor needs to be closed.
For example the following script
import mariadb
conn= mariadb.connect()
cursor1= conn.cursor()
cursor1.execute("select 1 from dual")
cursor2= conn.cursor()
cursor2.execute("select 2 from dual")
will throw an exception Mariadb.InterfaceError: Commands out of sync; you can't run this command now.
To avoid this, you need to create a buffered cursor:
cursor1= conn.cursor(buffered=True)
Im using this website: https://remotemysql.com/ for a sql database,
when I try to update value using sql console on phpmyadmin it works:
UPDATE users SET id='someid' WHERE username='myusername';
but with python mysql connector it doesnt:
import mysql.connector
mydb = mysql.connector.connect(
host="remotemysql.com",
user="blahblah",
password="blahblah",
database="blahblah",
)
mycursor = mydb.cursor()
mycursor.execute("UPDATE users SET id='someid' WHERE username='myusername';")
mydb.close()
the command is executed and doesnt throw an error, but in phpmyadmin theres no visible change, other commands like reading the data work.
Im asking how to make it work?
There are two possible reasons.
You should use mycursor.commit() after your statement, since it's a DML statement.
No rows match your where clause.
I am currently learning how to use Pyodbc for python and have got a good understanding of how it works. However, I have one question regarding getting the current sql console results. I understand you can use cursor.fetch...() commands to return queries, but how to you return a print line? For example:
SQL Version:
print convert(varchar, getdate(), 14)
Pyodbc Version:
cursor.execute('print convert(varchar, getdate(), 14)')
In sql editor this works, but I can not seem to get pyodbc to return print results? Is there a certain cursor command I need to use or some form of logic I need to create so pyodbc can understand a print line?
Just fetch the results and print. Replace print with select. Cursor only deals with the result set
cursor.execute('select convert(varchar, getdate(), 14)')
row = cursor.fetchone()
if row:
print(row)
I have the following script running;
from os import getenv
import pyodbc
import os
import sys
cnxn = pyodbc.connect('''
DRIVER={ODBC Driver 13 for SQL Server};SERVER=myServer\SQLEXPRESS;
DATABASE=myTable;UID=myID;PWD=myPassword''')
cursor = cnxn.cursor() #makes connection
cursor.execute("""
UPDATE ShowroomCal
SET ShowroomCal.isbusy = 'Yes'
FROM ShowroomCal
JOIN calendarbookings on calendarbookings.date=showroomcal.style112 AND calendarbookings.showroom=ShowroomCal.showroom_name
WHERE CalendarBookings.date = showroomcal.style112 and ShowroomCal.Year='2018'
""") #runs update statement
row_count = cursor.rowcount #counts afrfected rows
status_msg = "result returned. {} row(s) affected."
print(status_msg.format(row_count)) #prints affected rows
cnxn.close()
The notebook cell returns "3 row(s) affected." yet the table being updated doesn't change when viewed in SQL Express.
I have since independently run the statement from SQL Server Management in a Query Window and it instantly changed the three rows that would have been affected so I know that the data in the table exists to be changed and that the statement works - at least from a SQL standpoint.
Can someone spot any errors from a Python perspective?
Scratch'N'Purr provided the answer for me, thanks very much.
If anyone else is having the same issue it's the lack of
cnxn.commit()
After the statement that is the problem. I have since tested this and it worked perfectly.