99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
2020-08-28 閱讀量: 18566
pyhton連接數(shù)據(jù)庫后使用sql代碼如何查詢數(shù)據(jù)庫所有表名

之前一直使用python連接數(shù)據(jù)庫 ,用sql代碼查詢,一直是使用哪個表找哪個表,現(xiàn)在想看看數(shù)據(jù)庫里面都有哪些表,但是show tables不管用,想問下怎樣查詢出所有表名

35.2889
7
關注作者
收藏
評論(7)

發(fā)表評論
wangxishi
2020-09-01

哦哦 是這樣,問題解決了,postgreSQL用的 select * from pg_tables 語句,謝謝

0.0000 0 0 回復
wangxishi
2020-09-01

請問是怎么看出來連的是postgreSQL數(shù)據(jù)庫呢?

0.0000 0 0 回復
PGC123
2020-09-01

import psycopg2 as pg,導入的這個包就是用來操作postgreSQL數(shù)據(jù)庫的呀。我之前操作MySQL數(shù)據(jù)庫用的是pymysql這個包。不同的數(shù)據(jù)庫有不同的工具包來用的。

39.9511 2 0 回復
wangxishi
2020-09-01

import psycopg2 as pg

import pandas as pd

import numpy as np

import zipfile

import os

import sys

pd.set_option('display.max_columns',None)

pd.set_option('display.max_rows',None)

conn = pg.connect("dbname=%s user=%s password=%s host=%s port=%s" %('gpdw', 'dm_mtgg','A&eXbDuM7OiGz9BilS9X','gp-wz9q87z01t1bu4fv6.gpdb.rds.aliyuncs.com','3432'))

n_cursor = conn.cursor()

n_cursor = conn.cursor()


n = n_cursor.execute("show tables;")


for i in range(n):

info = n_cursor.fetchone()

print(info)

這是全部代碼,然后運行出來報錯如下:

1.png

0.0000 0 0 回復
PGC123
2020-09-01

show tables;是MySQL的用法。but 你的數(shù)據(jù)庫是postgreSQL,不是MySQL。所以查詢會報錯。你需要查看postgreSQL的用法

0.0854 1 0 回復
wangxishi
2020-09-01

請問什么叫查詢出錯沒有處理呢,關閉重新打開可以解決嗎?還有怎么使用commit方法或者回滾rollback處理之前的錯誤呢?

0.0000 0 0 回復
PGC123
2020-09-01

你需要把所有代碼發(fā)出來才行,只有局部代碼不好看出來具體的問題

0.0000 0 0 回復
wangxishi
2020-08-31

我這個提示錯誤是什么原因呢,如下圖:

1.png

0.0000 0 0 回復
PGC123
2020-08-31

之前有查詢出錯沒有處理導致的。可以使用commit方法或者回滾rollback處理之前的錯誤。然后就可以正常使用了

0.0000 0 0 回復
wangxishi
2020-08-31
cur = conn.cursor()  # 游標對象

這步?jīng)]有理解,什么叫游標對象

0.0000 0 0 回復
PGC123
2020-08-31

游標可以想象成一輛卡車,載著你的SQL命令去數(shù)據(jù)庫取數(shù)據(jù),然后返回。游標主要是用來裝載數(shù)據(jù)的,我們使用某個游標來請求(查詢)數(shù)據(jù),那么數(shù)據(jù)也跟著這個游標對象一起返回。

0.0000 0 0 回復
PGC123
2020-08-28

這個用python是可以實現(xiàn)的呀。我做了個簡單測試

我數(shù)據(jù)庫表的情況如下:

image.png


代碼如下:

import pymysql

MYSQL_CONFIG = {
    'host': 'localhost',  # IP地址
    'port': 3306,  # 端口
    'user': 'root',  # 用戶名
    'passwd': '1234',  # 密碼
    'db': 'test',  # 數(shù)據(jù)庫
    'charset': 'utf8',  # 編碼
}

conn = pymysql.connect(**MYSQL_CONFIG)  # 數(shù)據(jù)庫連接
cur = conn.cursor()  # 游標對象

n = cur.execute("show tables;")

for i in range(n):
    print(f'--這是第{i + 1}條數(shù)據(jù)--')
    info = cur.fetchone()
    print(info)
    
 ###########返回的打印結果###########
--這是第1條數(shù)據(jù)--
('dept',)
--這是第2條數(shù)據(jù)--
('emp',)
--這是第3條數(shù)據(jù)--
('emp1',)
--這是第4條數(shù)據(jù)--
('new',)
--這是第5條數(shù)據(jù)--
('orderd',)
--這是第6條數(shù)據(jù)--
('product',)
--這是第7條數(shù)據(jù)--
('salgrade',)
--這是第8條數(shù)據(jù)--
('store',)





35.2889 1 0 回復
推薦帖子
條評論