简体   繁体   English

如何与 Python-Mysql 交互

[英]How to interact with Python-Mysql

I have done the following code and I would like to ask the user to input how many new records want and after to fill column by column those records.我已经完成了以下代码,我想请用户输入需要多少条新记录,然后逐列填充这些记录。

import MySQLdb

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="Adam!977",
  database="testdb1"

)

cur = mydb.cursor()

get_tables_statement = """SHOW TABLES"""
cur.execute(get_tables_statement)
tables = cur.fetchall()
table = tables(gene)        
x=input("How many records you desire: ")
x
print "Please enter the data you would like to insert into table %s" %(table)
columns = []
values = []
for j in xrange(0, len(gene)):
    column = gene[j][0]
    value = raw_input("Value to insert for column '%s'?"%(gene[j][0]))
    columns.append(str(column))
    values.append('"' + str(value) + '"')
columns = ','.join(columns)
values = ','.join(values)
print columns
print values

The error that i get is about table gene( The table exist in db of SQL) Traceback (most recent call last): File "C:\\Users\\Admin\\Desktop\\π.py", line 25, in table = tables(gene) NameError: name 'gene' is not defined我得到的错误是关于表基因(该表存在于 SQL 的数据库中)回溯(最近一次调用最后一次):文件“C:\\Users\\Admin\\Desktop\\π.py”,第 25 行,表 = 表(基因)名称错误:名称“基因”未定义

Also, even I don't know if working properly the code.另外,即使我不知道代码是否正常工作。 Please, I need help.请,我需要帮助。 Thank you谢谢

The error being returned by python is down to the lack of definition of a variable gene . python 返回的错误归结为缺少变量gene的定义。 In the following line you reference gene , without it existing:在以下行中,您引用了gene ,但不存在它:

table = tables(gene)

In the documentation for the python mysql connector, under cursor.fetchall() you'll notice that this method returns either a list of tuples or an empty list.在 python mysql 连接器的文档中,在cursor.fetchall()您会注意到此方法返回元组列表或空列表。 It is therefore somewhat puzzling why you call tables as a function and attempt to pass a parameter to it - this is not correct syntax for accessing a list, or a tuple.因此,为什么将tables作为函数调用并尝试向其传递参数有点令人费解 - 这不是访问列表或元组的正确语法。

At the beginning of your code example you fetch a list of all of the tables in your database, despite knowing that you only want to update a specific table.在代码示例的开头,您获取了数据库中所有表的列表,尽管您知道只想更新特定表。 It would make more sense to simply reference the name of the table in your SQL query, rather than querying all of the tables that exist and then in python selecting one.在 SQL 查询中简单地引用表的名称会更有意义,而不是查询所有存在的表,然后在 python 中选择一个。 For example, the following query would give you 10 records from the table 'gene':例如,以下查询将从表 'gene' 中获取 10 条记录:

SELECT * FROM gene LIMIT 10

Below is an attempt to correct your code:以下是更正您的代码的尝试:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="Adam!977",
  database="testdb1"

)

x=input("How many records you desire: ")
cur = mydb.cursor()

get_rows_statement = """SELECT * FROM gene"""
cur.execute(get_rows_statement)
results = cur.fetchall()

This should give you all of the rows within the table.这应该为您提供表中的所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM