Help with Variables and SQL Syntax

Just starting out? Need help? Post your questions and find answers here.
jim.richards
New User
New User
Posts: 4
Joined: Mon Jun 23, 2003 12:31 pm
Location: Kingston, NH

Help with Variables and SQL Syntax

Post by jim.richards »

Hi All,

I need some guidance on the proper syntax to use in SQL statements when using variables.

This is what I am trying, which fails:

SQLString$ = "'"+fname$+"','"+lname$+"'"
Result = SQLiteExec("Insert Into contacts(fname, lname) Values("+SQLString$+");")

Thanks
Jim
Jim Richards
jrichards@necronline.com
(603) 893-2091
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Jim, try single quotes around text vars. Edit: sorry, looks like you do already, stupid me. Next suggestion: check vars do not include single quotes.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

You've found a strange bug, i'll fix it for next version (yes, Karbon, I'm on it ;). Put the SQL string in a single string variable as a workaround:

Code: Select all

SQLString$ = "'"+fname$+"','"+lname$+"'"
SQL$ = "INSERT INTO contacts VALUES("+SQLString$+")"
Result = SQLiteExec(SQL$)
Test code:

Code: Select all

#SQ_OK = 0
If InitSQLite()
  If SQLiteOpen("test.db")
    Result = SQLiteExec("CREATE TABLE contacts (fname, lname)") 
    If Result<>#SQ_OK:Debug SQLiteError(Result):EndIf
    fname$ = "uno"
    lname$ = "dos"
    SQLString$ = "'"+fname$+"', '"+lname$+"'"
    SQL$ = "INSERT INTO contacts VALUES("+SQLString$+")"
    Result = SQLiteExec(SQL$)
    If Result<>#SQ_OK:Debug SQLiteError(Result):EndIf
    Result = SQLiteGetTable("SELECT fname, lname FROM contacts")
    If Result<>#SQ_OK:Debug SQLiteError(Result):EndIf
    Debug SQLiteData(0, 0)
    Debug SQLiteData(0, 1)
    Debug SQLiteData(1, 0)
    Debug SQLiteData(1, 1)
    SQLiteClose()
    DeleteFile("test.db")
  EndIf
EndIf
Last edited by El_Choni on Sat Apr 03, 2004 4:21 pm, edited 1 time in total.
El_Choni
jim.richards
New User
New User
Posts: 4
Joined: Mon Jun 23, 2003 12:31 pm
Location: Kingston, NH

Thanks

Post by jim.richards »

Thanks Guys,

Works fine putting entire statement into a variable. I thought I was really losing it for a while there.

Jim
Jim Richards
jrichards@necronline.com
(603) 893-2091
Post Reply