how to connect mysql database in python using pymysql,KEY ERROR - python

im pretty new with python,im sorry if my question and my english doesnt sound right at all.
ive been trying to integrated my scraping output to mysql,but unfortunately i got stuck.can you good people help me?
the output/error
*<pymysql.cursors.Cursor object at 0x000000E81FF095E0>
<pymysql.connections.Connection object at 0x000000E81FEF9070>
KeyError Traceback (most recent call last)
in
9 data = csv.DictReader(scrapeddata)
10 for row in data:
---> 11 sql = "INSERT INTO emas ( tanggal, terakhir, pembukaan, tertinggi, terendah, vol, perubahan%) VALUES ('%s','%s','%s','%s','%s','%s','%s')" %(str(row["tanggal"]),str(row["terakhir"]),str(row["pembukaan"]),str(row["tertinggi"]),str(row["terendah"]),str(row["vol"]),str(row["perubahan%"]))
12 print(sql)
13 cur.execute(sql)
KeyError: 'tanggal'*
this is my code
import csv
import pymysql
#Connecting to MySQL in Windows
conn = pymysql.connect(host="127.0.0.1", port = 3306, user = "root", passwd = '', database = "mysql",
charset = "utf8")
cur = conn.cursor()
cur.execute("USE historis")
print(cur)
print(conn)
with open(r'C:\Users\shulhan\output_emas.csv') as scrapeddata:
data = csv.DictReader(scrapeddata)
for row in data:
sql = "INSERT INTO emas ( tanggal, terakhir, pembukaan, tertinggi, terendah, vol, perubahan%)
VALUES ('%s','%s','%s','%s','%s','%s','%s')"
%(str(row["tanggal"]),str(row["terakhir"]),str(row["pembukaan"]),str(row["tertinggi"]),str(row["terendah"]),str(row["vol"]),str(row["perubahan%"]))
print(sql)
cur.execute(sql)
conn.commit()

i think your connection setup should look like this:
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
you can find more information here:
https://pymysql.readthedocs.io/en/latest/user/examples.html

Related

pymysql error message (INSERT INTO function)

I encountered error message (INVALID SYNTAX) and have no idea why it happens.
Please help to figure it out.
import pymysql
db = pymysql.connect(host='localhost', port=3306, user='root', passwd='xxxxxxxx', db='ecommerce', charset='utf8')
ecommerce = db.cursor()
for index in range(10):
product_code = 215673140 + index +1
sql=INSERT INTO product VALUES(str(product_code),'sample data1','sample date2','sample data3'); ----> here's error point.
ecommerce.execute(sql)
db.commit()
db.close()
Try this. You have to enclose INSERT INTO ... inside " ". Also, don't use ; after the statement.
I suggest you should use %s. That way, you can insert any value inside that column
import pymysql
db = pymysql.connect(host='localhost', port=3306, user='root', passwd='xxxxxxxx', db='ecommerce', charset='utf8')
ecommerce = db.cursor()
for index in range(10):
product_code = 215673140 + index +1
sql = "INSERT INTO product VALUES (%s, %s,%s,%s,%s)"
val = (str(product_code),'sample data1','sample date2','sample data3')
ecommerce.execute(sql, val)
db.commit()
db.close()

how to connect MySQL database to input function

I'm building a program, where the user input an enterprise's name to get the code running.
I would like the user to pick the enterprise name in a drop down list, but currently i dont have a GUI to make it work.
so for now, I would like to connect the input function to the SQL database enterprises name column.
I already connected SQL to my python code... but how do I connect MySQL to an input function? I want the user to be able to get the information from the database
MY SQL CONNECTION :
import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="nn", passwd="passpass")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM listedatabase.entreprises_inspecteurs")
for row in mycursor.fetchall():
print (row[1])
mydb.close()
python:
enterprise_name = input()
Thank you for helping
I tried this but no luck:
mydb = mysql.connector.connect(host="localhost", user="nn", passwd="passpass")
mycursor = mydb.cursor()
enterprise_name = input()
mycursor.execute("SELECT n_fabricant FROM listedatabase.entreprises_inspecteurs", (enterprise_name,))
data = mycursor.fetchall()
if data:
code goes there
else:
print('data not found in database')
mydb = mysql.connector.connect(host="localhost", user="nn", passwd="passpass")
mycursor = mydb.cursor()
enterprise_name = input()
mycursor.execute("""SELECT n_fabricant FROM listedatabase.entreprises_inspecteurs WHERE n_fabricant = %s"""(event_inspecteur,))
data = mycursor.fetchall()
if data:
code goes there
else:
print('data not found in database')

Import CSV to SQL Express using Python

I'm trying to take a csv file and import it into sql server express using Python. I've googled the problem and the solution I'm seeing that seems to be working for everyone else is:
import pymssql
import csv
conn = pymssql.connect(
server="servername",
user='username',
password='password',
database='db'
)
cursor = conn.cursor()
with open('new_csv_file.csv', 'r') as f:
reader = csv.reader(f)
columns = next(reader)
query = 'insert into MyTable({0}) values ({1})'
query = query.format(','.join(columns), ','.join('?' * len(columns)))
cursor = conn.cursor()
for data in reader:
values = map((lambda x: x.strip()), data) # No need for the quote
print(tuple(values))
cursor.execute(query, tuple(values))
cursor.commit()
conn.commit()
cursor.close()
print("Done")
conn.close()
I've confirmed the code works it's just the "execute()" part doesn't.
query is ok, but "values" is giving me the following error:
(102, b"Incorrect syntax near 'Year'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")
Where 'Year' is the column I'm trying to fit the data in.
Thanks.
OK I found a work-around using sqlalchemy and pandas
import sqlalchemy
print('Creating Engine')
engine = sqlalchemy.create_engine('mssql+pymssql://username:password#localhost/db')
print('Reading Table')
df = pd.read_sql_table("table_name",engine)
print(df)
df_new = pd.read_csv('table_name_new.csv'%)
df_new.to_sql(name="table_name",
con = engine,
if_exists='replace'
)
df_test = pd.read_sql_table("table_name",engine)
If someone did find out why my code doesn't work I'm still interested. I don't know if it's a Python 3 issue (I'm using Python 3).
Cheers

Retrieve data from sql server database using Python

I am trying to execute the following script. but I don't get neither the desired results nor a error message ,and I can't figure out where I'm doing wrong.
import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=mySRVERNAME;"
"Database=MYDB;"
"uid=sa;pwd=MYPWD;"
"Trusted_Connection=yes;")
cursor = cnxn.cursor()
cursor.execute('select DISTINCT firstname,lastname,coalesce(middlename,\' \') as middlename from Person.Person')
for row in cursor:
print('row = %r' % (row,))
any ideas ? any help is appreciated :)
You have to use a fetch method along with cursor. For Example
for row in cursor.fetchall():
print('row = %r' % (row,))
EDIT :
The fetchall function returns all remaining rows in a list.
If there are no rows, an empty list is returned.
If there are a lot of rows, *this will use a lot of memory.*
Unread rows are stored by the database driver in a compact format and are often sent in batches from the database server.
Reading in only the rows you need at one time will save a lot of memory.
If we are going to process the rows one at a time, we can use the cursor itself as an interator
Moreover we can simplify it since cursor.execute() always returns a cursor :
for row in cursor.execute("select bla, anotherbla from blabla"):
print row.bla, row.anotherbla
Documentation
I found this information useful to retrieve data from SQL database to python as a data frame.
import pandas as pd
import pymssql
con = pymssql.connect(server='use-et-aiml-cloudforte-aiops- db.database.windows.net',user='login_username',password='login_password',database='database_name')
cursor = con.cursor()
query = "SELECT * FROM <TABLE_NAME>"
cursor.execute(query)
df = pd.read_sql(query, con)
con.close()
df
import mysql.connector as mc
connection creation
conn = mc.connect(host='localhost', user='root', passwd='password')
print(conn)
#create cursor object
cur = conn.cursor()
print(cur)
cur.execute('show databases')
for i in cur:
print(i)
query = "Select * from employee_performance.employ_mod_recent"
emp_data = pd.read_sql(query, conn)
emp_data

Export Python Dataframe to SQL Table

I'm trying to export a python dataframe to a SQL Server table.
Is there a better way to do this? I'm getting errors.
Dataframe - results_out
Output SQL table - FraudCheckOutput
cnn_out = pyodbc.connect('driver={SQL Server};server=XYZ;database=BulkLog;uid=sa;pwd=test')
results_out.to_sql(con=cnn_out, name='FraudCheckOutput', if_exists='replace', flavor='sqlite_master')
Thanks.
Ok, this supposed to make the work done:
import pypyodbc
def database_insert(query, params=())
conn_params = 'driver={SQL Server};server=XYZ;database=BulkLog;uid=sa;pwd=test'
try:
conn = pypyodbc.connect(conn_params)
except pypyodbc.Error, e:
print str(e)
else:
if conn.connected:
db = conn.cursor()
db.execute(query, params).commit()
finally:
if conn:
conn.close()
SQL_INSERT_QUERY = """
INSERT INTO table_name (
[field_name1],
[field_name2]
)
VALUES (
1,
'vale string'
)
WHERE
field_name3 = ?
"""
database_insert(SQL_INSERT_QUERY, ('field_name3_value',))
in pyodbc usage is very similar
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()
cursor.execute("insert into products(id, name) values ('pyodbc', 'awesome library')")
cnxn.commit()
more on http://code.google.com/p/pyodbc/wiki/GettingStarted

Categories

Resources