連線資料庫,並進行基礎的增刪改查

2021-10-09 19:22:40 字數 1189 閱讀 7632

import pymysql# 連線資料庫# pymysql.connect("資料庫伺服器位址", "使用者名稱", "密碼", "庫名", charset='utf-8')

database = pymysql.connect("127.0.0.1", "root", "123456", "mysql", charset='utf-8')

# 初始化指標

cursor = database.cursor()# 增加資料# 格式:"insert into 表明 (欄位1,欄位2,欄位3) values ('內容1','內容2','內容3');"

sql = "insert into date (date,company,private,price,weight) values ('2019-9-20','河北糧食','河北','2200','35.1');"

cursor.execute(sql)

# 對儲存的資料修改後,需要commit

database.commit()

database.close()# 修改資料# 格式:"update 表名 set 字段1=內容1,欄位2=內容2 where 條件;"

sql1 = "update data set date='2018-09-21' where date='2019-02-21';"

cursor.execute(sql1)

database.commit()

database.close()# 查詢資料# 格式:"select 字段 from 表名 where 條件;"

sql2 = "select company,price from date where date='2019-02-21';"

cursor.execute(sql2)

result = cursor.fetchall()

print(result)# 刪除資料# 格式:"delete from 表名 where 條件;"

sql3 = "delete from data where date='2018-08-08';"

cursor.execute(sql3)

database.commit()

database.close()

連線資料庫並進行增刪改查

public class jdbcutil catch classnotfoundexception e 資料庫連線 return public static connection geconnection catch sqlexception e return connection 資料庫增刪改 ...

QT 實現連線資料庫,並進行簡單的增刪改查等功能

這個小的程式實現主要分為兩個三個部分 新增乙個connection.件,實現資料庫的連線 在ui介面拖入相應的控制項,構件圖形化介面 在mainwindow.cpp中現實相應的 操作 需要注意函式都是按鍵的槽的clicked 函式 在實現專案前,首先需要在.pro中新增 qt sql ifndef ...

php連線資料庫增刪改查

header content type text html charset utf 8 1.建立和資料庫的連線 第乙個引數 連線資料的主機 第二個引數 連線資料的賬號 第三個引數 密碼 第四個引數 資料庫 預設連線的是3306 conn newmysqli localhost root ishop ...