Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我一直试图将mysqldatabase转换为html,但列表超出了范围_Python_Html_Mysql - Fatal编程技术网

Python 我一直试图将mysqldatabase转换为html,但列表超出了范围

Python 我一直试图将mysqldatabase转换为html,但列表超出了范围,python,html,mysql,Python,Html,Mysql,我不明白为什么它总是给我sys.argv[1]的列表超出范围错误。据我所知,我正在将数据传递给用户_数据库。请帮忙 import sys, MySQLdb def PrintFields(database, table): host = 'localhost' user = 'root' password = 'boysnblue1' conn = MySQLdb.Connection(db=parking_report, host=localhost, use

我不明白为什么它总是给我sys.argv[1]的列表超出范围错误。据我所知,我正在将数据传递给用户_数据库。请帮忙

import sys, MySQLdb

def PrintFields(database, table):
    host = 'localhost'
    user = 'root'
    password = 'boysnblue1'
    conn = MySQLdb.Connection(db=parking_report, host=localhost, user=root, passwd=boysnblue1)
    mysql = conn.cursor()
    sql = """ SHOW COLUMNS FROM %s """ % table
    mysql.execute("select id, date, time, status, from report_table ")
    fields=mysql.fetchall()
    print '<table border="0"><tr><th>order</th><th>name</th><th>type</th><th>description</th></tr>'
    print '<tbody>'
    counter = 0
    for field in fields:
        counter = counter + 1
        id = field[0]
        date = field[1]
        time = field[2]
        status = field[3]
        print '<tr><td>' + str(counter) + '</td><td>' + id + '</td><td>' + date + '</td><td>' + time + '</td><td>' + status + ' </td></tr>'
    print '</tbody>'
    print '</table>'
    mysql.close()
    conn.close()

users_database = sys.argv[1]
users_table = sys.argv[2]
print "Wikified HTML for " + users_database + "." + users_table
print "========================"
PrintFields(users_database, users_table)
导入系统,MySQLdb def打印字段(数据库、表): 主机='localhost' 用户='root' 密码='boysnblue1' conn=MySQLdb.Connection(db=parking\u report,host=localhost,user=root,passwd=boysnblue1) mysql=conn.cursor() sql=“”显示来自%s”“%表的列 execute(“从报表中选择id、日期、时间、状态”) fields=mysql.fetchall() 打印“ordernametypedescription” 打印“ 计数器=0 对于字段中的字段: 计数器=计数器+1 id=字段[0] 日期=字段[1] 时间=字段[2] 状态=字段[3] 打印“”+str(计数器)+''+id++''+date++''+time++''+status++'' 打印“ 打印“ mysql.close() 康涅狄格州关闭 users\u database=sys.argv[1] 用户表=sys.argv[2] 打印“Wikified HTML for”+用户\数据库+“+用户\表格” 打印“=====================================” 打印字段(用户\数据库、用户\表)
sys.argv
是一个列表,其中包含程序文件名及其在命令行上传递的所有参数

如果运行
pythonscript2.py
sys.argv
的内容将是
['script2.py']

如果运行
python script2.py database\u name table\u name
sys.argv
的内容将是
['script2.py'、'database\u name'、'table\u name']
,这是您的程序当前配置的预期内容:

users_database = sys.argv[1]
users_table = sys.argv[2]

由于您是以第一种方式调用它,
sys.argv[1]
不存在,因此您会得到一个错误,即索引(1)超出范围(它只指向0)。

sys.argv[1]
表示第一个命令行参数。你把它称为
python your_file.py some_arg some_other_arg
?请原谅我的无知,我不知道你的意思。你是如何调用你发布的脚本的?你是如何运行你的程序的?我使用python script2.py运行它。那么,你有你的答案了。运行它会给我带来一系列错误,这有助于我解决问题。非常感谢。