Python Forum
Inserting new row into SQLite
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inserting new row into SQLite
#1
While trying to INSERT into SQLite, can anyone advise on the reason for the error syntax error given in the example CODE 1 while CODE 2 and CODE 2 function as intended.


def AddToClpBrd():
    print (' AddToClpBrd')
    sqlite_file = 'mydatabase.sqlite'     # name of the sqlite database file
    table_name1 = 'VendClpBrd'             # name of the table to be creat
    
    id_col   = 'rowid'
    col_01   = 'Vend_01'

    ClpIns = 10       # Hard code for test
    CX = StringVar()
    #CX = Entbuy1.get() # For Code 1
    CX= '1234567890'   # For Code 2
    print(CX)

    
      # Connecting to the database file
            
    #  +++++++ CODE 1 This code below Produces the error
    ###  File "C:\MyScripts\Vendors.py", line 522, in AddToClpBrd
    ###  format(tn=table_name1, idf=id_col, cn=col_01))
    ###  sqlite3.OperationalError: near "BASCO": syntax error
    ###  'Where BASCO is the leading part of text from the Entry widget Entbuy1.get() 
      
    #conn = sqlite3.connect('/Users/Kevin/Documents/mydatabase.sqlite')
    #c = conn.cursor()
    #STRR = "INSERT OR IGNORE INTO {tn} ({idf}, {cn}) VALUES ( "+ str(ClpIns) +"," + CX + ")"
    #c.execute(STRR.\
    #          format(tn=table_name1, idf=id_col, cn=col_01))


     #  +++++++ CODE2 This code functions without error
    #conn = sqlite3.connect('/Users/Kevin/Documents/mydatabase.sqlite')
    #c = conn.cursor()
    #STRR = "INSERT OR IGNORE INTO {tn} ({idf}, {cn}) VALUES ( "+ str(ClpIns) +",' Test data ')"
    #c.execute(STRR.\
    #          format(tn=table_name1, idf=id_col, cn=col_01))
   


     #  +++++++ CODE3 This code functions without error
    conn = sqlite3.connect('/Users/Kevin/Documents/mydatabase.sqlite')
    c = conn.cursor()
    STRR = "INSERT OR IGNORE INTO {tn} ({idf}, {cn}) VALUES ( "+ str(ClpIns) +"," + CX +")"
    c.execute(STRR.\
              format(tn=table_name1, idf=id_col, cn=col_01))  

   #  From  Sebastian Raschka
   # c.execute("INSERT OR IGNORE INTO {tn} ({idf}, {cn}) VALUES (123456, 'test')".\
   #     format(tn=table_name, idf=id_column, cn=column_name))
  
    conn.commit()
    conn.close()
Reply


Messages In This Thread
Inserting new row into SQLite - by KevinBrown - Mar-27-2019, 07:46 PM
RE: Inserting new row into SQLite - by nilamo - Mar-27-2019, 09:50 PM
RE: Inserting new row into SQLite - by KevinBrown - Mar-28-2019, 03:09 AM
RE: Inserting new row into SQLite - by KevinBrown - Mar-28-2019, 09:07 AM
RE: Inserting new row into SQLite - by KevinBrown - Mar-28-2019, 10:19 AM
RE: Inserting new row into SQLite - by nilamo - Mar-28-2019, 07:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Inserting values from multiple lists sqlite azulu 1 2,518 May-24-2020, 08:40 AM
Last Post: ibreeden
  Problem with inserting a string in to Sqlite db darktitan 3 4,537 Mar-03-2019, 06:30 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020