MySQL Forums
Forum List  »  Connector/Python

Re: Connector for python 3.5 in Windows
Posted by: Enda Keane
Date: May 18, 2017 04:25AM

Is this your problem ,
You cannot use
# connect to mysql database program.

import mysql.connector
cnx = mysql.connector.connect(user = 'test', host = '127.0.0.1', database = 'dbname')
cursor = cnx.cursor()

# Querting my database
query = (" SELECT * FROM tablename")
cursor.execute(query)

for (cy) in cursor:
print('{}'.format(cy))

cursor.close()
cnx.close()

if so , than I suggest you use the following as I had to :


import MySQLdb

db = MySQLdb.connect(host="localhost",user="root",passwd="yourpassword here",db="your database name here")

cur = db.cursor()

cur.execute("SELECT * FROM your DBtablename")

#Querying my database
# open a file handler for the file to write to the database
fo= open('RawData1.csv','w')

for row in cur.fetchall():
output = (row[5] + ',' + row[6]+ ',' + row[7] + ',' + row[8] + ',' + row[9] + ',' + row[17] + ',' + row[17])
print(output)
fo.write(output)
db.close()

Hope this helps

Options: ReplyQuote


Subject
Written By
Posted
Re: Connector for python 3.5 in Windows
May 18, 2017 04:25AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.