sqlite3 syntax help

This is the place for queries that don't fit in any of the other categories.

sqlite3 syntax help

Postby charisma_tubagus » Thu Sep 26, 2013 5:16 am

hi all,

I tried to do a simple a database task using sqlite3 in python3. My code is like this:

Code: Select all
import sqlite3

def create(db,tablename):
    db.execute('drop table if exists ?', (tablename,))
    db.execute('create table ?(name text, age int)', (tablename,))

def main():
    conn = sqlite3.connect('example.db')
    create(conn, 'test')

main()   


but this gives me the following error:
sqlite3.OperationalError: near "?": syntax error

I would like to know what goes wrong in my code. Thanks. :D
Last edited by Mekire on Thu Sep 26, 2013 5:37 am, edited 1 time in total.
Reason: First post lock.
charisma_tubagus
 
Posts: 2
Joined: Thu Sep 26, 2013 5:10 am

Re: sqlite3 syntax help

Postby stranac » Thu Sep 26, 2013 7:41 am

I believe(but I could be wrong), that the ? syntax is only used for passing parameters.
I think you need to use string formatting for stuff like this.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1135
Joined: Thu Feb 07, 2013 3:42 pm

Re: sqlite3 syntax help

Postby charisma_tubagus » Tue Oct 01, 2013 11:12 am

You mean by using .format() method of the string?
or using %s place holder?
charisma_tubagus
 
Posts: 2
Joined: Thu Sep 26, 2013 5:10 am

Re: sqlite3 syntax help

Postby stranac » Tue Oct 01, 2013 6:53 pm

They both do the same thing
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1135
Joined: Thu Feb 07, 2013 3:42 pm

Re: sqlite3 syntax help

Postby micseydel » Tue Oct 01, 2013 6:56 pm

charisma_tubagus wrote:You mean by using .format() method of the string?
or using %s place holder?

.format() is the new way, and in later version of Python 3 they may remove the %s stuff. I tend to prefer .format(), but %s is still well-know, especially because programmers from a C-based language will instantly know what's going on.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1295
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests