Connect python to mysql instance sharing, pythonmysql instance
Example 1
# Coding = UTF-8import sysimport MySQLdbimport timereload (sys) sys. setdefaultencoding ('utf-8') def connectDemo (): return MySQLdb. connection ("127.0.0.1", "root", "root", "demo", 3306, charset = "utf8 ") if _ name _ = '_ main _': begin = time. time () conn = connectDemo () cursor = conn. cursor () SQL = "" show tables "count = cursor.exe cute (SQL) rows = cursor. fetchall () cursor. close () conn. close () print "======== demo library Total: % s sheets of tables ================" % (count) print 'time consumed: % s second' % (time. time ()-begin)
Example 2
import MySQLdbconn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="test")cursor = conn.cursor()cursor.execute("select * from hard")res = cursor.fetchall()for x in res:print xcursor.close()conn.close()
Example 3
1. Install the Python Mysql package
root@10.1.1.45:~# apt-get install python-mysqldb root@10.1.1.45:~# python Python 2.5.2 (r252:60911, Jan 4 2009, 21:59:32) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
If no error is reported when importing MySQLdb, the installation is successful.
2. You can connect to the database and perform addition, deletion, and modification.
Root@10.1.1.45: python # cat create. py #! /Usr/bin/env python # coding = UTF-8 # import related modules import MySQLdb # establish a connection with the mysql database conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = 'davehe ') # obtain the cursor curs = conn. cursor () # Run the SQL statement to create a database named curs.exe cute ("create database pythondb") # select the database to connect. select_db ('pythondb') # Execute SQL to create a table named curs.exe cute ("create table test (id int, message varchar (50 ))") # insert a record value = [1, "davehe"] curs.exe cute ("insert into test values (% s, % s)", value) # insert multiple records values = [] for I in range (20): values. append (I, 'Hello mysqldb' + str (I) curs.exe cute.pdf ("insert into test values (% s, % s)", values) # submit to modify conn. commit () # Close the cursor connection and release the resource curs. close () # close the connection conn. close () root@10.1.1.45: python #. /create. py
3. Use python to view the newly added records in mysql.
Root@10.1.1.45: python # cat select. py #! /Usr/bin/env python # coding = UTF-8 # import related modules import MySQLdb # establish a connection with the mysql database conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = 'hc1226') # obtain the cursor curs = conn. cursor () # select the database to connect to conn. select_db ('pythondb') # Check the total number of records count = curs.exe cute ('select * from test') print "A total of % s records" % count # Get a record, returns result = curs as a tuple. fetchone () print "current record ID: % s message: % s" % result # obtain the last 10 records. Because getchone () was previously executed (), so the cursor has pointed to the second record, and then results = curs is returned from the second record. fetchults (10) for r in results: print r # reset the cursor position, 0, as the offset, mode = relative (default) curs. scroll (0, mode = 'absolute ') # obtain all records results = curs. fetchall () for r in results: print r # submit and modify conn. commit () # Close the cursor connection and release the resource curs. close () # close the connection conn. close ()
Root@10.1.1.45: python #. /select. py has a total of 21 record current record IDs: 1 message: davehe (0L, 'Hello mysqldb0') (1L, 'Hello mysqldb1') (2L, 'Hello mysqldb2 ') (3L, 'Hello mysqldb3') (4L, 'Hello mysqldb4') (5L, 'Hello mysqldb5') (6L, 'Hello mysqldb6') (7L, 'Hello mysqldb7 ') (8L, 'Hello mysqldb8') (9L, 'Hello mysqldb9') (1L, 'davehe ') (0L, 'Hello mysqldb0') (1L, 'Hello mysqldb1 ') (2L, 'Hello mysqldb') (3L, 'Hello mysqldb3') (4L, 'Hello mysqldb4') (5L, 'Hello mysqldb5') (6L, 'Hello mysqldb6 ') (7L, 'Hello mysqldb7') (8L, 'Hello mysqldb8') (9L, 'Hello mysqldb9') (10L, 'Hello mysqldb10') (11L, 'Hello mysqldb11 ') (12L, 'Hello mysqldb12') (13L, 'Hello mysqldb13') (14L, 'Hello mysqldb14') (15L, 'Hello mysqldb15') (16L, 'Hello mysqldb16 ') (17L, 'Hello mysqldb17') (18L, 'Hello mysqldb18') (19L, 'Hello mysqldb19 ')