3 Replies - 224 Views - Last Post: 11 August 2014 - 12:14 PM Rate Topic: -----

#1 happyjedi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-August 14

Trying to use sqlite3 module and not sure of my syntax mistakes

Posted 10 August 2014 - 03:52 AM

First things first, I'm new to python, but have been programming in other languages for years, but I'm not a full time developer, so please be gentle ;-)

I'm trying to update records in a mysqlite database, and all works well, except when I want to pass the type of update as a parameter to a function so I can refactor the code. The highlighted code in the large code segment is where the problem lies. I pass in three parameters to the function, and python just errors pointing at the first question mark. If you look at the code below that sets up the parameters to pass to the function - the operat_type name can be set to DESC,COMMAND or NAME.

I have been trying all sort's of combinations to get that parameter to take - but nothing works. Here is a line that does work, when I don't try to put a variable in the type area:

[b]place.execute('UPDATE srvs SET DESC=? WHERE ID=?', (new_desc_name,id_name))[/b]



Now I'm doing something wrong, so any guidance to correct this will help me progress

Thanks in advance


import sys
from sqlite3 import dbapi2 as sqlite
db = sqlite.connect('srvs-data.db')
place = db.cursor()

def update_record(one,two,three):
    print("New name for server {} to be put into DB {} {}:".format(one,two,three)) 
    [b]place.execute('UPDATE srvs SET ?=? WHERE ID=?', (one,two,three))[/b]


operat_type = str(input("comand type"))   
new_desc_name = str(input("New description for record {}: ".format(id_name)))
update_record(operat_type,new_desc_name,id_name)



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to use sqlite3 module and not sure of my syntax mistakes

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,273
  • Joined: 12-June 08

Re: Trying to use sqlite3 module and not sure of my syntax mistakes

Posted 10 August 2014 - 08:20 AM

08	    place.execute('UPDATE srvs SET ?=? WHERE ID=?', (one,two,three))

I believe the parameter list is surrounded by brackets [ ] not parentheses ( ) .
Was This Post Helpful? 0
  • +
  • -

#3 happyjedi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-August 14

Re: Trying to use sqlite3 module and not sure of my syntax mistakes

Posted 10 August 2014 - 09:36 AM

Thank you for the reply, but I get the same error message when I try [] instead of ().

Traceback (most recent call last):
File "/Users/nickhadd/bin/srvs-sqllite-modify-python.py", line 59, in <module>
update_record(operat_type,new_desc_name,id_name)
File "/Users/nickhadd/bin/srvs-sqllite-modify-python.py", line 15, in update_record
place.execute('UPDATE srvs SET ?=? WHERE ID=?', [a,b,c])
sqlite3.OperationalError: near "?": syntax error


Any other ideas gratefully received.

Cheers

Nick
Was This Post Helpful? 0
  • +
  • -

#4 happyjedi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-August 14

Re: Trying to use sqlite3 module and not sure of my syntax mistakes

Posted 11 August 2014 - 12:14 PM

Ok, I have the answer to this question , it is to do with string parameter passing, and the sqllite parameter passing not being one and the same.

The correct line should now be:

place.execute("UPDATE srvs SET {}=? WHERE ID=?".format(a), (b, c))


Notice the a parameter is passwd into the {} for the string, and the sqllite commands are passed in within the ?.

Glad I found a pythonista to point to this , as it's not obvious , but makes sense now.

Not sure how you mark this topic as resolved - but it can now be so marked.

This post has been edited by modi123_1: 11 August 2014 - 12:15 PM
Reason for edit:: fixed botched code tag

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1