0

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

Also, even I don't know if working properly the code. Please, I need help. Thank you

2
  • Any particular reason for using Python 2? Commented Nov 27, 2019 at 11:10
  • Is the main direction from the Msc that I follow Commented Nov 27, 2019 at 11:29

1 Answer 1

1

The error being returned by python is down to the lack of definition of a variable gene. In the following line you reference gene, without it existing:

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. 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.

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. For example, the following query would give you 10 records from the table 'gene':

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank for your answer. So I should delete table=tables(gene)? But I dont know how I am gonna connect the table gene in the inputs of an user. Could you fix me the code please?
I'm not suggesting deleting just one statement, so much as I am suggesting you change the way that you are intending to get your records from the table. I will update my answer with how I believe you should do it.
Thank you Sir! I appreciate!
Hi Adam, You should be able to take the code I have provided as an exemplar and adapt it to your purposes. If you treat the results variable like the gene variable in your loop then you ought to be able to loop through all the values within the table and print out the columns. If you are struggling with this, try printing out the contents of results to see the datastructure and how you might handle it and loop through it. Some reading of the documentation may help you also.
It depends what you want to be printed out to the user, are you intending for the program to say "Please enter the data you would like to insert into table gene" ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.