0

私はデータベースにまったく慣れていないので、 http://halfcooked.com/presentations/osdc2006/python_databases.htmlにある便利なガイドを使用して簡単なものをまとめましたが、理解できないエラーが返されます。

try:
    from sqlite3 import dbapi2 as sqlite
except ImportError:
    from pysqlite2 import dbapi2 as sqlite

db_connection = sqlite.connect('program.db')

db_curs = db_connection.cursor()

def create_customer(cID, fname, sname, dob):
    db_curs.execute("CREATE TABLE " + cID + " ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
    db_curs.execute("INSERT INTO " + cID + " (first_name, last_name, date_of_birth) VALUES (fname, sname, dob)")
    db_connection.commit()
    db_curs.execute("SELECT * FROM " + cID )

create_customer("1", "John", "Farnham", "12/08/95")
create_customer("1", "Indianna", "Jones", "05/05/95")


print db_curs.fetchall()

私が受け取っているエラーは次のとおりです。

Traceback (most recent call last):
  File "C:\Users\fin0005\Documents\loyalty.py", line 17, in <module>
    create_customer("1", "John", "Farnham", "12/08/95")
  File "C:\Users\fin0005\Documents\loyalty.py", line 12, in create_customer
    db_curs.execute("CREATE TABLE " + cID + " ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
OperationalError: near "1": syntax error
4

1 に答える 1

1

テーブル名の周りにバッククォートを追加して、テーブル名として整数を作成しているとは思わないようにします

def create_customer(cID, fname, sname, dob):
    db_curs.execute("CREATE TABLE `" + cID + "` ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
    db_curs.execute("INSERT INTO `" + cID + "` (first_name, last_name, date_of_birth) VALUES (fname, sname, dob)")
    db_connection.commit()
    db_curs.execute("SELECT * FROM `" + cID  + "`")

# In SQL terms, the following blows up
# create table 2 (id int(10) PRIMARY KEY); Due to the 2 being an integer
# create table `2` (id int(10) PRIMARY KEY); Works, due to the 2 being properly identified with backticks :)

# Here's some code as requested in the comment, everything below this point is a self contained example, please do not copy the function above
def initiate_customer_table(table_name):
    db_curs.execute("CREATE TABLE IF NOT EXISTS `" + table_name + "` ( id INTEGER PRIMARY KEY, first_name VARCHAR(20),last_name VARCHAR(30), date_of_birth DATE)")
    db_connection.commit()       

def create_customer(table_name, fname, sname, dob):
    db_curs.execute("INSERT INTO `" + table_name + "` (first_name, last_name, date_of_birth) VALUES (%s, %s, %s)", [fname, sname, dob])
    db_connection.commit()

    # Fetches the user just created
    db_curs.execute("SELECT * FROM `" + table_name  + "` WHERE id = %s", [db_curs.insert_id()])

    # Returns the user
    return db_curs.fetchone()


desired_table_name = 'customers'

initiate_customer_table(desired_table_name)

customer_1 = create_customer(desired_table_name, "Bryan", "Moyles", "1800-01-01")
customer_2 = create_customer(desired_table_name, "Awesome", "Guy", "1800-01-01")

また、本番環境でこのコードを使用する予定がある場合は、さらに一歩進んで、すべてのフィールドがmysqlに対して適切にエスケープされるようにすることをお勧めします。

于 2012-05-05T06:00:21.927 に答える