+ 13

How to link python to sql database?

I want to fetch data from database.

16th Jan 2019, 9:59 PM
AKS
AKS - avatar
4 Réponses
+ 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())
16th Jan 2019, 10:19 PM
Nicolas Poulain
Nicolas Poulain - avatar
+ 7
You need to install and import the correct library for the database, there are different connectors for MySQL, PostgreSQL, MS SQL, Oracle, Sqlite...
17th Jan 2019, 7:07 AM
Tibor Santa
Tibor Santa - avatar
+ 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
20th Jan 2019, 9:15 AM
Shadab Husain
Shadab Husain - avatar