-1
# -*- 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 连接,但我无法使用该程序打印表名,有人可以帮我吗?非常感谢 !

4

1 回答 1

1

您粘贴了无法产生您所说的错误的部分代码。这是重写的版本:

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

运行这个,只有这个

于 2012-09-20T05:57:17.417 回答