string class variable to sqlite3
i'm mainly a PHP man but playing with python recently!
I have a very small class that retrieves data from a very small sqlite3 db
called encyclopedia,which has a table called wiki & two field called one & two
(yes I know - no imagination, I should get out more!):
import sqlite3
class do:
def doConn(self):
self.conn = sqlite3.connect('encyclopedia')
self.myText = "sulphur"
print "Opened database successfully";
cursor = self.conn.execute("SELECT * from wiki WHERE one LIKE
'alan turing' ")
for row in cursor:
print "first field = ", row[0]
print "second filed = ", row[1]
print "Operation done successfully";
self.conn.close()
x = do()
x.doConn()
#the above works when I pass a string as an argument such as the above where I
use 'alan turing'
i want to pass an argument as a variable which in PHP could be $somevariable or
$this->somevariable which say equal "some string"
I have played around with passing self.myText instead of 'alan turing' and it
doesn't like it- oh Alan I wish you were here!
--
https://mail.python.org/mailman/listinfo/python-list
Re: string class variable to sqlite3
cheers Mark,
it was the syntax that was foxing me; I don't like the example of the select
statement via the insecure approach because the writer is adding an element of
ambiguity since the table has a field called and a variable also
called
#modifying my class to
import sqlite3
class do:
myString1 = 'uranium'
myString2 = ('lead',)
def doConn(self):
self.conn = sqlite3.connect('encyclopedia')
print "Opened database successfully"
myString3 = 'sulphur'
cursor = self.conn.execute("SELECT * from wiki WHERE one LIKE
'%s' "% self.myString1)
#cursor = self.conn.execute("SELECT * from wiki WHERE one =?",
self.myString2)
#cursor = self.conn.execute("SELECT * from wiki WHERE one LIKE
'%s'" % myString3 )
for row in cursor:
print "ID = ", row[0]
print "NAME = ", row[1]
print "Operation done successfully"
self.conn.close()
x = do()
x.doConn()
#all select statements work , the first two strings are defined under class so
referencing with 'self' works. other is defined inside method so doesn't need
it.
--
https://mail.python.org/mailman/listinfo/python-list
Re: string class variable to sqlite3
I acknowledge the security points & also by the way I omitted using any Try Catch statements , because at this stage coming from PHP I was more focused on getting a select statement to actually work in python. Also I know the end use will be off line and is part of a python & kivy project to android via buildozer ,for an off line searchable version of the digital encyclopedia from SOSchildren. PHP version here: http://www.ginbrookesfoundation.org/showArticle/70 I'm just glad I have half a clue now! Loving python so far anyway! -- https://mail.python.org/mailman/listinfo/python-list
