python 鏈結資料庫及鏈結遇到的問題(一)

2021-10-02 01:54:16 字數 1608 閱讀 2169

import pymysql
host =

'127.0.0.1'

# 資料庫的ip

user =

'gyadmin'

#登入資料庫的賬號

password =

"test112233"

#資料庫的密碼

port =

3306

# 埠號

database =

"gy_core"

# 連線的庫名

charset =

"utf8"

# 設定傳參的格式

open_sql = pymysql.connect(host=host, 

user=user,

password=password,

port=port,

database=database,

charset=charset)

import pymysql

host =

'127.0.0.1'

# 資料庫的ip

user =

'gyadmin'

#登入資料庫的賬號

password =

"test112233"

#資料庫的密碼

port =

3306

# 埠號

database =

"gy_core"

# 連線的庫名

charset =

"utf8"

# 設定傳參的格式

print

(type

(host)

)print

(type

(port)

)open_sql = pymysql.connect(host=host,

user=user,

password=password,

port=port,

database=database,

charset=charset)

# 開啟mysql的游標功能,建立乙個游標物件

cursor = open_sql.cursor(

)# 查詢語句

sql1 =

"select * from sys_auth_module"

# 使用游標物件執行sql語句;

cursor.execute(sql1)

# 使用fetchone()方法,獲取返回的結果,但是需要用變數儲存返回結果;

data = cursor.fetchone(

)print

(data)

# 斷開資料庫的連線,釋放資源;

open_sql.close(

)

鏈結是報錯:typeerror: %d format: a number is required, not str

這個問題查了好久,終於最後知道,port 引數(埠號)不能使用引號

類似於:

port =

"3306"

python鏈結mysql資料庫

安裝mysql python包 sudo apt get install python mysqldb在程式中使用 db connect url,user,password,database cursor db.cursor 獲取游標 sql create table employee first ...

python鏈結MySQL資料庫

python鏈結mysql資料庫。用到mysqldb庫,安裝 pip install mysql python 我這邊安裝完報錯 error command c program files microsoft visual studio 14.0 vc bin cl.exe failed with ...

Python 鏈結MySQL資料庫

參考資料 這裡使用的是庫是pymysql python鏈結資料庫時,mysql中要有要連線的資料庫,否則會無法連線。import pymysql import movie host localhost 主機名 user root 使用者名稱 password 密碼 database douban2 ...