Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:08:27+00:00 2026-06-03T07:08:27+00:00

I’m completely new to databases, and have put something simple together using the helpful

  • 0

I’m completely new to databases, and have put something simple together using the helpful guide that can be located at http://halfcooked.com/presentations/osdc2006/python_databases.html , However it’s returning an error that I don’t understand

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()

the error I am receiving is as follows:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:08:28+00:00Added an answer on June 3, 2026 at 7:08 am

    Add backticks around your table name, so that it doesn’t think it’s creating an integer as a table name

    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")
    

    I will also suggest that you go a step further, if you plan on using this code in production, to ensure that all fields are escaped properly for mysql.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.