日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python databaseoperate_python开发_python操作mysql数据库

發(fā)布時(shí)間:2025/3/11 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python databaseoperate_python开发_python操作mysql数据库 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 #-*- coding: utf-8 -*-

2 #python operate mysql database

3 importMySQLdb4

5 #數(shù)據(jù)庫名稱

6 DATABASE_NAME = ''

7 #host = 'localhost' or '172.0.0.1'

8 HOST = ''

9 #端口號(hào)

10 PORT = ''

11 #用戶名稱

12 USER_NAME = ''

13 #數(shù)據(jù)庫密碼

14 PASSWORD = ''

15 #數(shù)據(jù)庫編碼

16 CHAR_SET = ''

17

18 #初始化參數(shù)

19 definit():20 globalDATABASE_NAME21 DATABASE_NAME = 'test'

22 globalHOST23 HOST = 'localhost'

24 globalPORT25 PORT = '3306'

26 globalUSER_NAME27 USER_NAME = 'root'

28 globalPASSWORD29 PASSWORD = 'root'

30 globalCHAR_SET31 CHAR_SET = 'utf8'

32

33 #獲取數(shù)據(jù)庫連接

34 defget_conn():35 init()36 return MySQLdb.connect(host = HOST, user = USER_NAME, passwd = PASSWORD, db = DATABASE_NAME, charset =CHAR_SET)37

38 #獲取cursor

39 defget_cursor(conn):40 returnconn.cursor()41

42 #關(guān)閉連接

43 defconn_close(conn):44 if conn !=None:45 conn.close()46

47 #關(guān)閉cursor

48 defcursor_close(cursor):49 if cursor !=None:50 cursor.close()51

52 #關(guān)閉所有

53 defclose(cursor, conn):54 cursor_close(cursor)55 conn_close(conn)56

57 #創(chuàng)建表

58 defcreate_table():59 sql = '''

60 CREATE TABLE `student` (61 `id` int(11) NOT NULL,62 `name` varchar(20) NOT NULL,63 `age` int(11) DEFAULT NULL,64 PRIMARY KEY (`id`),65 UNIQUE KEY `name` (`name`)66 ) ENGINE=InnoDB DEFAULT CHARSET=utf867 '''

68 conn =get_conn()69 cursor =get_cursor(conn)70 result =cursor.execute(sql)71 conn.commit()72 close(cursor, conn)73 returnresult74

75 #查詢表信息

76 defquery_table(table_name):77 if table_name != '':78 sql = 'select * from' +table_name79 conn =get_conn()80 cursor =get_cursor(conn)81 result =cursor.execute(sql)82 for row incursor.fetchall():83 print(row)84 #for r in row: #循環(huán)每一條數(shù)據(jù)

85 #print(r)

86 close(cursor, conn)87 else:88 print('table name is empty!')89

90 #插入數(shù)據(jù)

91 definsert_table():92 sql = 'insert into student(id, name, age) values(%s, %s, %s)'

93 params = ('1', 'Hongten_a', '21')94 conn =get_conn()95 cursor =get_cursor(conn)96 result =cursor.execute(sql, params)97 conn.commit()98 close(cursor, conn)99 returnresult100

101 #更新數(shù)據(jù)

102 defupdate_table():103 sql = 'update student set name = %s where id = 1'

104 params = ('HONGTEN')105 conn =get_conn()106 cursor =get_cursor(conn)107 result =cursor.execute(sql, params)108 conn.commit()109 close(cursor, conn)110 returnresult111

112 #刪除數(shù)據(jù)

113 defdelete_data():114 sql = 'delete from student where id = %s'

115 params = ('1')116 conn =get_conn()117 cursor =get_cursor(conn)118 result =cursor.execute(sql, params)119 conn.commit()120 close(cursor, conn)121 returnresult122

123 #數(shù)據(jù)庫連接信息

124 defprint_info():125 print('數(shù)據(jù)庫連接信息:' + DATABASE_NAME + HOST + PORT + USER_NAME + PASSWORD +CHAR_SET)126

127 #打印出數(shù)據(jù)庫中表情況

128 defshow_databases():129 sql = 'show databases'

130 conn =get_conn()131 cursor =get_cursor(conn)132 result =cursor.execute(sql)133 for row incursor.fetchall():134 print(row)135

136 #數(shù)據(jù)庫中表情況

137 defshow_tables():138 sql = 'show tables'

139 conn =get_conn()140 cursor =get_cursor(conn)141 result =cursor.execute(sql)142 for row incursor.fetchall():143 print(row)144

145

146 defmain():147 show_tables()148 #創(chuàng)建表

149 result =create_table()150 print(result)151 #查詢表

152 query_table('student')153 #插入數(shù)據(jù)

154 print(insert_table())155 print('插入數(shù)據(jù)后....')156 query_table('student')157 #更新數(shù)據(jù)

158 print(update_table())159 print('更新數(shù)據(jù)后....')160 query_table('student')161 #刪除數(shù)據(jù)

162 delete_data()163 print('刪除數(shù)據(jù)后....')164 query_table('student')165 print_info()166 #數(shù)據(jù)庫中表情況

167 show_tables()168

169

170 if __name__ == '__main__':171 main()

總結(jié)

以上是生活随笔為你收集整理的python databaseoperate_python开发_python操作mysql数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。