python MySQLdb包 增刪改查簡單應用

2022-05-26 21:21:10 字數 4137 閱讀 9913

1

#! /usr/bin/env python2#

-*- coding: utf-8 -*-

3import

mysqldb45

class

database:

6def

__init__

(self,user,passwd,db,host,port):

7 self.user=user

8 self.passwd=passwd

9 self.db=db

10 self.host=host

11 self.port=port

12try

:13 self.conn=mysqldb.connect(user=self.user,passwd=self.passwd,db=self.db,host=self.host,port=self.port)

14 self.cur=self.conn.cursor()

15except

mysqldb.error as e:

16print

(e)17

18def

is_user_exists(self,username):

19 sql="

select salary from employee where name='%s';

" %username

20try:21

self.cur.execute(sql)

22 data=self.cur.fetchall()

23if len(data)==0:

24return

false

25else:26

return

true

27except

mysqldb.error as e:

28print

(e)2930#

查詢資料

31def

opt_select(self,username):

32 sql="

select salary from employee where name='%s';

" %username

33try:34

self.cur.execute(sql)

35 data=self.cur.fetchall()

36for i in

data:

37print ('

工資: %d

') %(i[0])

38print('

查詢資料')

39except

mysqldb.error as e:

40print

(e)4142#

插入資料

43def

opt_insert(self,username,value):

44 sql='

insert into employee(name,salary) values(\'%s\',%s);

' %(username,value)

45try:46

self.cur.execute(sql)

47self.conn.commit()

48print ('

插入資料')

49except

mysqldb.error as e:

50print

(e)5152#

修改員工工資

53def

opt_update(self,username,value):

54 sql='

update employee set salary=%s where name=\'%s\';

' %(value,username)

55try:56

self.cur.execute(sql)

57self.conn.commit()

58print ('

修改資料')

59except

mysqldb.error as e:

60print

(e)6162#

刪除員工資料

63def

opt_delete(self,username):

64 sql='

delete from employee where name=\'%s\';

' %username

65try:66

self.cur.execute(sql)

67self.conn.commit()

68print('

刪除資料')

69except

mysqldb.error as e:

70print

(e)71

72def

close_conn(self):

73try:74

self.cur.close()

75self.conn.close()

76except

mysqldb.error as e:

77print

(e)78

7980 tips='''

8182

1: 查詢

832: 新增

843: 修改

854: 刪除

865: 退出

8788

請選擇:

89'''

90 options=[1,2,3,4,5]

9192

if__name__=='

__main__':

93 conn_mysql=database('

root

','1

','test

','localhost

',3306)

94while

true: 95#

選擇操作

96try

:97 choose=input(tips)

98except

(eoferror,keyboardinterrupt):

99 choose=5

100if choose not

inoptions:

101continue

102elif choose==1:

103 username = raw_input('

輸入要查詢員工姓名: ')

104if conn_mysql.is_user_exists(username) is

false:

105print ('

員工不存在')

106else

:107

conn_mysql.opt_select(username)

108elif choose==2:

109 username,value = raw_input('

輸入新增員工姓名,工資:

').split(','

)110

conn_mysql.opt_insert(username,value)

111elif choose==3:

112 username,value= raw_input('

輸入要更新員工的姓名,工資:

').split(','

)113

conn_mysql.opt_update(username,value)

114elif choose==4:

115 username= raw_input('

輸入要刪除員工的姓名: ')

116conn_mysql.opt_delete(username)

117elif choose==5:

118print('

退出程式......')

119 sys.exit()

python MySQLdb學習筆記

mysqldb庫是python訪問mysql的連線庫,最近專案中需要使用,將學習使用所得整理如下。mysqldb windows下執行需要 libmysql.dll libmmd.dll 和 libguide40.dll 可以放在sitepackage下也可以在windows system32 學習...

python MySQLdb連線mysql失敗問題

mysql exceptions.operationalerror 2002,can t connect to local mysql 在很多種情況下,如果配置檔案沒有出錯的話,將機器重啟,確認關閉防火牆,確定啟動了mysql即可。網上很不錯講解 最近了解了一下django,資料庫選用了mysql,...

Python Mysqldb使用簡介

python db api使用流程 一 python查詢mysql使用 fetchone 方法獲取單條資料,使用fetchall 方法獲取多條資料。fetchone 該方法獲取下乙個查詢結果集。結果集是乙個物件 fetchall 接收全部的返回結果行.rowcount 這是乙個唯讀屬性,並返回執行e...