+ 13
How to link python to sql database?
I want to fetch data from database.
4 Respuestas
+ 8
Something Like that ?
import MySQLdb
connection = MySQLdb.Connect(host='localhost', user='user', passwd='pwd', db='mydatabase')
cursor = connection.cursor()
query = "SELECT * FROM %s" % (mytable)
cursor.execute( query )
connection.commit()
print( cursor.fetchall())
+ 7
You need to install and import the correct library for the database, there are different connectors for MySQL, PostgreSQL, MS SQL, Oracle, Sqlite...
+ 1
You can use a connection object for calling methods like commit(), rollback() and close() as shown below:
>>>cur = conn.cursor() //creates new cursor object for executing SQL statements
>>>conn.commit() //Commits the transactions
>>>conn.rollback() //Roll back the transactions
>>>conn.close() //closes the connection
>>>conn.callproc(proc,param) //call stored procedure for execution
>>>conn.getsource(proc) //fetches stored procedure code