python操作mysql查詢資料

2021-07-11 03:34:39 字數 1342 閱讀 9043

首先需要連線資料庫,然後才查詢出資料。

例如下表名字為 sinauser

iduse_id

use_name11

db12

2db233

db3

class

database

:def

__init__

(self)

: self.conn= mysqldb.connect(

# 連線資料庫

host='',

# 連線你要取出資料庫的ip,如果是本機可以不用寫

port =

3306

, user=

'***'

,# 你的資料庫使用者名稱

passwd=

'******'

,# 你的資料庫密碼

db =

'sina'

, charset=

'utf8',)

self.user_list =

defget_mysql_user

(self,begin,end)

:#取出資料庫

with self.conn:

# 獲取連線上的字典cursor,注意獲取的方法,

# 每乙個cursor其實都是cursor的子類

cur = self.conn.cursor(mysqldb.cursors.dictcursor)

# 執行mysql語句,這裡獲取id從begin 到 end 的資料

cur.execute(

"select * from sinauser where id >= "

+str

(begin)

+" and id <= "

+str

(end)

)# 獲取資料方法

rows = cur.fetchall(

)# 遍歷資料(比上乙個更直接一點)

dict_uid =

dict()

# print rows

for row in rows:

# 這裡,可以使用鍵值對的方法,由鍵名字來獲取資料

# print "%s %s" % (row["user_id"], row["user_name"])

# print "%s" % (row["user_id"])

"user_id"])

# print user_list

# dict_uid[row["user_id"]]=row["user_name"]

return self.user_list

Python操作MySQL 變數查詢操作

不知道是不是我找得不對,找了好幾個網上關於python對mysql變數查詢操作沒有用。甚至抄官方文件用法 do this insteadt rhat c execute select from stocks where symbol t printc.fetchone larger example ...

python操作mysql 插入 查詢 刪除

塊語法遵循標準markdown 例如 usr bin env python coding utf 8 mysqldb查詢 import mysqldb conn mysqldb.connect host localhost port 3306,user root passwd 44570253 db...

MySQL查詢操作

mysql查詢語句 select from 表名 查詢出該表名下所有資料 代表所有字段 簡單的查詢語句方式 select 字段列表 表示式 函式 from 表名 查詢多個字段 select 欄位1,欄位2 from 表名 表示式 select 表示式 算術表示式 from 表名 例如 此處有一張工資...