mysql與python的互動

2021-08-13 03:45:34 字數 4440 閱讀 8765

conn = connect(引數列表)

cursor1=conn.cursor()

mode表示移動的方式

mode的預設值為relative,表示基於當前行移動到value,value為正則向下移動,value為負則向上移動

mode的值為absolute,表示基於第一條資料的位置,第一條資料位置為零

-建立testinsert.py檔案,向學生表中插入一條資料

#encoding = utf-8

import mysqldb

try:

conn=mysqldb.conect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf-8')

cs1=conn.cursor()

count=cs1.execute("insert into students(sname) values('張亮')")

print(count)

conn.commit()

cs1.close()

except exception, e:

print(e.message)

-建立testinsert.py檔案,修改學生表中一條資料

#encoding = utf-8

import mysqldb

try:

conn=mysqldb.conect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf-8')

cs1=conn.cursor()

count=cs1.execute("update students set sname='楊國富'where id=6")

print(count)

conn.commit()

cs1.close()

except exception, e:

print(e.message)

-建立testinsert.py檔案,刪除學生表中一條資料

#encoding = utf-8

import mysqldb

try:

conn=mysqldb.conect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf-8')

cs1=conn.cursor()

count=cs1.execute("delete from students where id=6")

print(count)

conn.commit()

cs1.close()

except exception, e:

print(e.message)

#encoding= utf-8

import mysqldb

try:

conn = mysqldb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf-8')

cs1=conn.cursor()

sname=input("請輸入學生姓名:")

params=[sname]

count=cs1.excute('insert into students(sname)value(%s)',params)

print(count)

cs1.close()

conn.close()

except exception,e:

print e.message

#encoding=utf8

import mysqldb

try:

conn=mysqldb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')

cur=conn.sursor()

cur.excude('select * from students where id=7')

result=cur.fetchone()

print(result)

cur.close()

conn.close()

except exception,e:

print(e.message)

#encoding=utf8

import mysqldb

try:

conn=mysqldb.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')

cur=conn.sursor()

cur.excude('select * from students where id=7')

result=cur.fetchall()

print(result)

cur.close()

conn.close()

except exception,e:

print(e.message)

#encoding=utf8

import mysqldb

class

mysqlhelper

():def

__init__

(self,host,port,db,user,passwd,charset='utf8'):

self.host=host

self.port=port

self.db=db

self.user=user

self.passwd=passwd

self.charset = charset

defconnect

(self):

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

self.cursor=self.conn.cursor()

defclose

(self):

self.cursor.close()

self.conn.close()

defget_one

(self,sql,params=()):

result=none

try:

self.connect()

self.cursor.excute(sql,params)

result = self.cursor.fetchone()

self.close()

except exception,e:

print(e.message)

return result

defget_all

(self,sql,params=()):

list=()

try:

self.connect()

self.cursor.execute(sql,params)

list=self.cursor.fetchall()

self.close()

except exception,e:

print(e.message)

return list

definsert

(self,sql,params=()):

return self.__edit(sql,params)

defupdate

(self,sql,params=()):

return self.__edit(sql,params)

defdelete

(self,sql,params=()):

return self.__edit(sql,params)

def__edit

(self,sql,params):

count=0

try:

self.connect()

count=self.cursor.execute(sql,params)

self.conn.commit()

self.close()

except exception,e:

print(e.message)

return count

python與MySQL的互動

要想和mysql資料庫互動,首先需要安裝資料庫驅動模組,python2和python3的資料庫驅動是不同的。python2中的資料庫模組是mysqldb,可以通過以下命令安裝 sudo apt get install python mysql在檔案中引入模組 import mysqldbpython...

python與mysql的互動

python 中操作mysql步驟 1.匯入pymsq,from pymysql import 2.建立 和資料庫之間的網路通路 conn connect host localhost port3306,database jing dong user root password mysql char...

mysql與python的互動

mysql是一種關係型資料庫,是為了表示事物與事物之間的關係,本身存於資料庫中的內容意義並不大,所以廣泛應用於程式語言中,python中九含有與mysql互動的模組 pymysql 程式設計對mysql的操作 首先需要引入pymysql模組 import pymysql 連線資料庫 一般需要幾個引數...