关于传递参数打印Mysql表名使用mysqldb问题?

2024-06-08 06:33:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

import os
import time
import re
import urllib
import urllib2
import MySQLdb

hostip = raw_input("Input your sql Ip:")
username = raw_input("Input your sql username:")
password = raw_input("Input your password:")
dbname = raw_input("Input your dbname :")

conn = MySQLdb.connect(host = hostip, user = username, passwd = password, db = dbname)
cur = conn.cursor()
cur.execute("show tables")
alltable = cur.fetchall()
tablenames = []
for i in range(len(alltable)):
    for j in range(len(alltable[i])):
        tablenames.append(alltable[i][j])

print tablenames

上面是我的代码,需要通过输入参数来访问mysql connect,但是我不能打印表名使用这个程序,有人能帮我吗? 非常感谢你!你知道吗


Tags: importinputsqlyourrawusernamepasswordconn
1条回答
网友
1楼 · 发布于 2024-06-08 06:33:54

您粘贴了部分无法产生所述错误的代码。以下是重新编写的版本:

import MySQLdb
from getpass import getpass

hostip = raw_input("Input your sql Ip:")
username = raw_input("Input your sql username:")
password = getpass("Input your password:")
dbname = raw_input("Input your dbname :")

conn = MySQLdb.connect(hostip, username, password, dbname)
cur = conn.cursor()
cur.execute("SHOW TABLES")
tablenames = [i[0] for i in cur.fetchall()]
print tablenames

运行这个并且只运行这个。你知道吗

相关问题 更多 >

    热门问题