python 與 MySQL數(shù)據(jù)庫

python 與 MySQL數(shù)據(jù)庫

入門

上代碼

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

import MySQLdb

import random

conn = MySQLdb.connect(

host='localhost',

port=3306,

user='root',

passwd='zeratel30000',

db='lhf_db_test_1',

)

cur = conn.cursor()

# 創(chuàng)建數(shù)據(jù)表

# cur.execute(

#? ? "CREATE TABLE colleague(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(20),class VARCHAR(30),age VARCHAR(10))CHARACTER SET utf8 COLLATE utf8_general_ci")

# 插入一條數(shù)據(jù)

# cur.execute("INSERT INTO colleague VALUES('2','Tom','3 year 2 class','9')")

# a = 1000

# while a > 0:

#? ? a -= 1

#? ? insert = "INSERT INTO colleague VALUES(null,'%s','%s','%s')" % (

#? ? ? ? ? random.choice(['qwe', 'asd', 'zxc', 'aqz', 'wsx', 'edc']),

#? ? ? ? random.choice(['apple', 'pear', 'peach', 'orange', 'lemon']), random.randint(1, 100))

#? ? cur.execute(insert)

# 修改查詢條件的數(shù)據(jù)

# executeStr = cur.execute("UPDATE colleague SET class='3 year 1 class' WHERE name = 'Tom'")

executeStr = cur.execute("SELECT * from colleague WHERE name='asd'")

print(executeStr)

i = 0

while i < executeStr:

i += 1

print(cur.fetchone())

# # 刪除查詢條件的數(shù)據(jù)

# cur.execute("DELETE FROM colleague WHERE age='9'")

cur.close()

conn.commit()

conn.close()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容