Python connection MySQL instance sharing _python

Source: Internet
Author: User

Example One

#coding =utf-8

Import sys
import mysqldb
import time

reload (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.execute (sql)
  rows = Cursor.fetchall ()
  cursor.close (
  conn.close ()
  print "========demo Library total:%s sheet ============"% (count)

  print ' time consuming:%s seconds '% (Time.time ()-begin)

Example Two

Import MYSQLDB
conn = mysqldb.connect (host= "localhost",
user= "root", passwd= "123456"
, db= "
Test ")
cursor = conn.cursor ()
cursor.execute (" SELECT * from Hard ")
res = Cursor.fetchall () to
X in Res:
print x
cursor.close ()
conn.close ()

Example Three

1 Installing 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, 4 2009, 21:59:32)  
[GCC 4.3.2] on linux2 
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information. 
>>> Import MySQLdb 
>>> 

There is no error in importing MYSQLDB here, which indicates that the installation was successful.

2 can be connected to the database, can be added to the deletion operation.

root@10.1.1.45:python# cat create.py  
#!/usr/bin/env python 
#coding =utf-8 
 
#导入相关模块 
Import mysqldb 
 
#建立和mysql数据库的连接 
conn = mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' Davehe ') 
#获取游标 
curs = Conn.cursor () 
#执行SQL, create a database 
curs.execute ("CREATE Database Pythondb") 
#选择连接哪个数据库 
Conn.select _db (' pythondb ') 
#执行SQL, create a table 
curs.execute ("CREATE TABLE test (ID int,message varchar)") 
#插入一条记录 
value = [1, "Davehe"] 
curs.execute (INSERT INTO test values (%s,%s), value) 
#插入多条记录 
values = [] For 
i in range: 
  values.append ((i, ' Hello MySQLdb ' + str (i)) 
Curs.executemany ("INSERT INTO Test VALUES (%s,%s) ", values) 
#提交修改                 
conn.commit () 
#关闭游标连接, releasing Resources 
Curs.close () 
#关闭连接 
Conn.close () 
root@10.1.1.45:python#./create.py 

3 below, use Python to view the records just added in MySQL.

 root@10.1.1.45:python# cat select.py #!/usr/bin/env python #coding =utf-8 #导入相关模块 im Port MYSQLDB #建立和mysql数据库的连接 conn = mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' hc1226 ') #获取游标 curs = conn.  Cursor () #选择连接哪个数据库 conn.select_db (' pythondb ') #查看共有多少条记录 count = Curs.execute (' SELECT * from Test ') print "Total%s record" % Count #获取一条记录, returns result = Curs.fetchone () print "current record id:%s message:%s"% #获取后10条记录 as a tuple, because Getchone was previously executed ( , so the cursor already points to the second record, and the following starts back with the second record results = Curs.fetchmany for R in Results:print R #重置游标位置, 0, offset, mode = relative (default) Curs.scroll (0,mode= ' absolute ') #获取所有记录 results = Curs.fetchall () for R in Results:print R #提交修改 Conn.comm It () #关闭游标连接, freeing Resources curs.close () #关闭连接 conn.close () 
 root@10.1.1.45:python#./select.py a total of 21 records the current record id:1 Message:davehe (0L, ' Hello my Sqldb0 ') (1L, ' Hello mysqldb1 ') (2L, ' Hello mysqldb2 ') (3L, ' Hello mysqldb3 ') (4L, ' Hello Mysqldb4 ') (5L, ' Hello mysq LDB5 ') (6L, ' Hello Mysqldb6 ') (7L, ' Hello Mysqldb7 ') (8L, ' Hello Mysqldb8 ') (9L, ' Hello mysqldb9 ') (1L, ' Davehe ') (0  L, ' 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 ') (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 ') 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.