自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

對Python操作方法說明

開發(fā) 后端
這種語言具有非常簡捷而清晰的語法特點,適合完成各種高層任務(wù),幾乎可以在所有的Python操作中運行,目前,基于這種語言的相關(guān)技術(shù)正在飛速的發(fā)展。

Mysql是一個優(yōu)秀的開源數(shù)據(jù)庫,它現(xiàn)在的應(yīng)用非常的廣泛,因此很有必要簡單的介紹一下用python操作mysql數(shù)據(jù)庫的方法。Python操作數(shù)據(jù)庫需要安裝一個第三方的模塊。

由于Python的數(shù)據(jù)庫模塊有專門的數(shù)據(jù)庫模塊的規(guī)范,所以,其實不管使用哪種數(shù)據(jù)庫的方法都大同小異的,這里就給出一段示范的Python操作代碼:

  1. #-*- encoding: gb2312 -*-  
  2. import os, sys, string  
  3. import MySQLdb  
  4.  
  5. # 連接數(shù)據(jù)庫   
  6. try:  
  7.     conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1')  
  8. except Exception, e:  
  9.     print e  
  10.     sys.exit()  
  11.  
  12. # 獲取cursor對象來進行操作  
  13.  
  14. cursor = conn.cursor()  
  15. # 創(chuàng)建表  
  16. sql = "create table if not exists test1(name varchar(128) primary key, age int(4))" 
  17. cursor.execute(sql)  
  18. # 插入數(shù)據(jù)  
  19. sql = "insert into test1(name, age) values ('%s', %d)" % ("zhaowei", 23)  
  20. try:  
  21.     cursor.execute(sql)  
  22. except Exception, e:  
  23.     print e  
  24.  
  25. sql = "insert into test1(name, age) values ('%s', %d)" % ("張三", 21)  
  26. try:  
  27.     cursor.execute(sql)  
  28. except Exception, e:  
  29.     print e  
  30. # 插入多條  
  31.  
  32. sql = "insert into test1(name, age) values (%s, %s)"   
  33. val = (("李四", 24), ("王五", 25), ("洪六", 26))  
  34. try:  
  35.     cursor.executemany(sql, val)  
  36. except Exception, e:  
  37.     print e  
  38.  
  39. #查詢出數(shù)據(jù)  
  40. sql = "select * from test1" 
  41. cursor.execute(sql)  
  42. alldata = cursor.fetchall()  
  43. # 如果有數(shù)據(jù)返回,就循環(huán)輸出, alldata是有個二維的列表  
  44. if alldata:  
  45.     for rec in alldata:  
  46.         print rec[0], rec[1]  
  47.  
  48.  
  49. cursor.close()  
  50.  
  51. conn.close() 

【編輯推薦】

  1. 如何使Python嵌入C++應(yīng)用程序?
  2. 深入探討Ruby與Python語法比較
  3. Python學習資料介紹分享
  4. Python學習經(jīng)驗談:版本、IDE選擇及編碼解決方案
  5. 淺析Python的GIL和線程安全
責任編輯:chenqingxiang 來源: 51CTO.com
相關(guān)推薦

2010-03-04 09:58:32

安裝Python

2010-03-05 13:48:24

Python for

2009-12-15 13:59:42

Ruby對象操作

2010-03-04 14:32:24

Python自動下載文

2010-03-15 15:18:23

Python運行

2010-02-01 10:40:13

Python Djan

2009-12-30 14:28:06

Silverlight

2010-02-23 17:59:52

WSIT連接WCF

2010-03-05 10:36:52

Python調(diào)用zip

2009-12-31 11:35:20

Silverlight

2009-09-18 10:58:31

C#數(shù)組操作

2010-03-11 09:56:57

Python字符串操作

2009-08-18 15:49:19

C# 操作Excel

2009-12-28 17:48:01

WPF界面布局

2011-03-29 10:16:47

Jave枚舉

2009-12-30 15:53:28

Silverlight

2010-08-25 14:37:38

snort入侵檢測

2010-09-08 15:47:08

JavsScriptJavaScript

2010-01-05 15:43:13

.NET Framew

2010-01-28 14:01:32

Android監(jiān)聽通話
點贊
收藏

51CTO技術(shù)棧公眾號