Python操作Mysql資料庫

2021-09-02 16:02:47 字數 885 閱讀 9918

1)python db api : python訪問資料庫的統一介面規範。

2)python databaseinte***ces:

2)測試一下mysqldb模組是否已經正常匯入。

>>> import mysqldb

如果沒有錯誤提示則表明已經正常匯入。

3)下面是可以參考的運算元據庫的python**。

#coding=utf-8

import mysqldb

conn= mysqldb.connect(

host='localhost',

port = 3306,

user='root',

passwd='123456',

db ='test',

)cur = conn.cursor()

#建立資料表

#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

#插入一條資料

#cur.execute("insert into student values('2','tom','3 year 2 class','9')")

#修改查詢條件的資料

#cur.execute("update student set class='3 year 1 class' where name = 'tom'")

#刪除查詢條件的資料

#cur.execute("delete from student where age='9'")

cur.close()

conn.commit()

conn.close()

python操作mysql查詢資料

首先需要連線資料庫,然後才查詢出資料。例如下表名字為 sinauser iduse id use name11 db12 2db233 db3class database def init self self.conn mysqldb.connect 連線資料庫 host 連線你要取出資料庫的ip,...

python操作MySQL資料庫

堅持每天學一點,每天積累一點點,作為自己每天的業餘收穫,這個文章是我在吃飯的期間寫的,利用自己零散的時間學了一下python操作mysql,所以整理一下。我採用的是mysqldb操作的mysql資料庫。先來乙個簡單的例子吧 import mysqldb try conn mysqldb.connec...

Python操作Mysql資料庫

coding utf8 author yangjing import mysqldb 查詢。def select host user root password port 3306,db sql connect mysqldb.connect host host,user user,passwd p...