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 8602425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:04:57+00:00 2026-06-12T02:04:57+00:00

I am trying to create a new table in SQLite3 (data coming from a

  • 0

I am trying to create a new table in SQLite3 (data coming from a txt file) in a Python script. I am creating a string to use as the SQL command that will give the column headers. However, when I insert the variable for this string into the cursor.execute() I get a syntax error. The thing is, if I print() and then copy and paste that into the cursor.execute, the script works. However, I want to use this same script for various txt files, so it needs to adapt to the column headers in the file.

def CreateTable (daDB, daFile, daTableName): #Creates a table with column headers as   first row of file
    con=sqlite3.connect(daDB)
    curs=con.cursor()
    LinesList=FileReadIn(daFile)
    Headers=LinesList[0]
    HeadersString=""
    for word in Headers:
        HeadersString += word + ", "
    DaHeadersString=HeadersString[0:-2]
    daQuery="'create table " + daTableName + " (" + DaHeadersString + ")'"
    print (daQuery)
    curs.execute(daQuery)
    con.commit()
    con.close()

This generates:

 CreateTableFromText ('animalsheaderstest.sqltdb','animals.txt','animalstable')
'create table animalstable (Owner, Name, Species)'

Traceback (most recent call last):
  File "<pyshell#108>", line 1, in <module>
   CreateTable ('animalsheaderstest.sqltdb','animals.txt','animalstable')
  File     "/Users/zeintawil/Zeins_Files/School/Senior/OPIM_399/python/practice/ReadInPrac.py", line     62, in CreateTable
   curs.execute(daQuery)
OperationalError: near "'create table animalstable (Owner, Name, Species)'": syntax error

However, when I copy and paste the value of print(daQuery) directly into the code, it works.

  • 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-12T02:04:58+00:00Added an answer on June 12, 2026 at 2:04 am

    Looks like you have used both single and double quotes. Try like this:

    daQuery="create table " + daTableName + " (" + DaHeadersString + ")"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to correctly write TSQL to create a new table containing the following
Im trying to create new file on D: drive with c/c++ I found this
I am trying to create a new table by copying an existing table in
I trying to make method to alter table. I've build data.db Created new group
I am trying to develop a class that I can use to create table/columns
I am now trying to create new database manager under Fragment class. But unfortunately,
I am trying to create new Event objects to be persisted in the database
I am trying to create new rules profile in Sonar 2.9 with my checkstyle
Trying to create a new Dedicated Cache Role in Windows Azure but get the
When trying to create a new team project with any of the default project

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.