I have written the following short and simple script in order to connect a MySQL database called mybase with Python. I have populated already the table users of my database with data in MySQL Workbench. The problem is that when I run the script, I see no results printed in the console, but only my cmd opening for one second and closing automatically. Could someone help me find out what I am doing wrong? This is my script:
import mysql.connector as mysql
db = mysql.connect(host='localhost',
database='mybase',
user='root',
password='xxx',
port= 3306)
cursor = db.cursor()
q= "SELECT*FROM users"
cursor.execute(q)
for row in cursor.fetchall():
print (row[0])
I appreciate any help you can provide!
Maybe its a syntax error with your query. It should be like this to work
q= "SELECT * FROM users"
If it does not work, what I found helps me it is to test the queries first in a client with a local database and then copying them into my code.
Related
I am connecting a Python program in Visual Studio Code to a SQL databse stored in MySQL Workbench 8.0. I am using the PyMySQL connector to do this. However, I am running into an error with code that I have used from another question that I have posted. Here is the link and the code:
How do I connect a Python program in Visual Studio Code to MySQL Workbench 8.0?
pip install PyMySQL
import pymysql
con = pymysql.Connect(
host='localhost',
port=3306,
user='root',
password='123456',
db='test',
charset='utf8'
)
cur = con.cursor()
sql1 = 'select * from student'
cur.execute(sql1)
data = cur.fetchall()
cur.close()
con.close()
for i in data:
print(str(i))
Here is a screenshot with my code and the error that I received.
I tried the code that I recieved from my previous question, but it resulted in an another error. I am pretty sure I have copied the code correctly and the database details. I have researched the error but have been unable to find its relevance to connecting Python programs to MYSQL Workbench 8.0 with PyMySQL.
First of all, obviously you didn't copy the code in the answer correctly.
Your code has the following errors (only from the picture, I don't know what your complete code looks like)
The port number is 3306. NOT 33060. Of course, if you make changes when you install the database, you need to change it to the port number you use.
The fetchall method in data = cur.fetchall does parentheses. It shoud be data = cur.fetchall().
At the moment it seems that the error in the picture is due to the port number.
Modifying to the correct port number will remove this error.
I have a sqlite3 DB with a few rows in it. When I try to fetch data from it in Python, fetchall returns an empty list.
con = sqlite3.connect("commands.db")
cursor = con.cursor()
con.execute("SELECT * FROM commands;")
existing = cursor.fetchall()
print(existing)
# Prints []
The data is being inserted in a different part of the project fine. I verified this by opening the DB in "DB Browser for SQLite" and running the following command. This returned the data in the table with no problem.
SELECT * FROM commands;
Has anyone come across a similar issue?
Thank you for the help in advance!
Just change con.execute to cursor.execute. Then it should work
I am using Impyla and Python in the CDSW to query data in HDFS and use it. The problem is sometimes to get all of the data I have to go in and manually click on the "Invalidate all metadata and rebuild index" button in HUE.
Is there a way to do this in the workbench with a library or python code?
I assume you are using something like this to connect to impala via impyla ... try executing the invalidate metadata <table_name> command
from impala.dbapi import connect
conn = connect(host='my.host.com', port=21050)
cursor = conn.cursor()
cursor.execute('INVALIDATE METADATA mytable') # run this
cursor.execute('SELECT * FROM mytable LIMIT 100')
print cursor.description # prints the result set's schema
results = cursor.fetchall()
I am having a difficult time trying to connect to a SQL Server DB on Linux, using pyodbc. I have a ODCINI file entry created. I started with this:
import pyodbc
conn = pyodbc.connect('DSN=DSN;Database=DB;UID=UID;PWD=PWD')
cursor = conn.cursor()
cursor.execute('SELECT count(*) FROM dbo.tableA')
for row in cursor.fetchall():
print(row)
which throws this error:
RuntimeError: Unable to set SQL_ATTR_CONNECTION_POOLING attribute.
I googled that error and added this line after reading some recommendations:
pyodbc.pooling=False
So script changed to this:
import pyodbc
pyodbc.pooling=False
conn = pyodbc.connect('DSN=DSN;Database=DB;UID=UID;PWD=PWD')
cursor = conn.cursor()
cursor.execute('SELECT count(*) FROM dbo.tableA')
for row in cursor.fetchall():
print(row)
Which resulted in this:
pyodbc.InterfaceError: ('IM003', '[IM003] 䑛瑡䑡物捥嵴佛䉄⁃楬嵢匠数楣楦摥搠楲敶\u2072潣汵\u2064潮⁴敢氠慯敤d\uffff\uffff㢸ꔻ罱\x00\ue5b8鮫罱\x00㳰ꔻ罱\x00\uffff\uffff罱\x00\x00\x00\x00\x00鳭ꕞ罱\x00塰ꕉ罱 (0) (SQLDriverConnect)')
At the suggestion of a coworker I added these 2 lines AFTER the pyodbc.connect line:
conn.setdecoding(pyodbc.SQL_CHAR, encoding='latin1', to=str)
conn.setencoding(str, encoding='latin1')
I tried that with both latin1 and utf-8. Neither work, still throws the same interface error with Chinese characters.
Any ideas?
I had the similar issue with same description RuntimeError: Unable to set SQL_ATTR_CONNECTION_POOLING attribute. I had no clue what is happening and why is happening. After lot of debugging i was able to figure it out why.
Simple answer is :
Reinstall the unixODBC drivers or/and SQL drivers.
Reason why :
When install the ODBC Drivers first and then SQL related drivers, sometimes it override the symlinks in Unix system. You can find out more info on this from pyodbc official GitHub issue#847 .
you can simply uninstall and then do:
conda install unixodbc
Im trying to insert a value in a python script, and im not getting any errors but it isnt displaying in the SQL file and I cant find any documentation that helps me understand why it isnt displaying.
Here is the code
#Connect the db to this python script
top10_connection = connect(database = 'top_ten.db')
#Get a cursor for the database which allows you to make
#and execute sql queries in this script
top_ten_db = top10_connection.cursor()
top_ten_db.execute("INSERT INTO Top_Ten (Rank) VALUES(1)")
top10_connection.close()
If you arent getting any traceback errors you are msot likely forgetting to commit changes to the db.
add top10_connection.commit() before you close the connection:
top10_connection = connect(database = 'top_ten.db')
#Get a cursor for the database which allows you to make
#and execute sql queries in this script
top_ten_db = top10_connection.cursor()
top_ten_db.execute("INSERT INTO Top_Ten (Rank) VALUES(1)")
top10_connection.commit()
top10_connection.close()