python獲取資料庫資訊

2021-09-26 08:41:42 字數 712 閱讀 7865

#親測可用

import pymysql

import sys

# 匯入pymysql模組

import pymysql

# 獲取連線

coon = pymysql.connect(host="localhost",

db="test",

user="root",

password="root",

port=3306,

charset="utf8")

# 獲取游標

cursor = coon.cursor()

sql = "select * from table where name=%s"

try:

username=sys.ar**[1]

print("----------",username)

# 返回值是影響的行數

count = cursor.execute(sql, (username,))

data = cursor.fetchall()

# 手動提交事務

coon.commit()

except exception as e:

print("----異常資訊:",e)

# 回滾資料

coon.rollback()

print("返回資訊",data)

print("同步推送資訊完成!")

獲取資料庫資訊

資料庫資訊包括資料庫詳細資訊 資料庫基本資訊 基本表資訊 列資訊等內容 1 獲取資料庫詳細資訊 databasemetadata物件代表了乙個資料庫的詳細資訊,它的方法所獲取的資料庫系統的資訊通常用resultset物件的開工返回,可以用resultset物件的方法取得資料資訊,如getstring...

python獲取MySQL資料庫資訊的步驟教程

分為五步,如下所示 1 連線資料庫 import pymysql 這裡的connect,也可以寫為connection和connect database pymysql.connect host localhost 如果是伺服器,則輸公網ip user root 當時設定的資料超級管理員賬戶 pas...

獲取資料庫表的有關資訊

1.獲取所有資料庫名 1 select name from master.dbo.sysdatabases order by name 2.獲取所有表名 1 select name from sysobjects where xtype u order by name xtype u 表示所有使用者...