[Tutor] insert problem using gadfly

rickp@telocity.com rickp@telocity.com
Thu, 8 Mar 2001 09:54:29 -0500


How can I insert a string that contains newlines into a gadfly database?
The docs say that most anything (ie, string, tuple, list, etc) can be
put in a varchar field so why does gadfly look at the contents of a
string? I could understand having to escape embedded quotes but not
embedded newlines.

Here is some example code:

import gadfly

try:
	conn = gadfly.gadfly('test_db','gadfly')
	curs = conn.cursor()
except:
	conn = gadfly.gadfly()
	conn.startup('test_db','gadfly')
	curs = conn.cursor()
	curs.execute('create table tstab (id integer, txt varchar)')

id = 1
txt = 'this is a single line of text'
sql = "insert into tstab (id,txt) values ('%s','%s')" % (id,txt)
curs.execute(sql)
print "successfully inserted a single line of text"
# the program works this far

id = 2
txt = """this is
multiple lines
of text"""
sql = "insert into tstab (id,txt) values ('%s','%s')" % (id,txt)
curs.execute(sql)
# at this point I get the following traceback:

Traceback (innermost last):
  File "./tstdb.py", line 25, in ?
    curs.execute(sql)
  File "/usr/lib/python1.5/site-packages/gadfly.py", line 322, in execute
    cs = self.commands = sql.DoParse1(statement, context)
  File "/usr/lib/python1.5/site-packages/kjParser.py", line 1175, in DoParse1
    return self.DoParse(String, Context, DoReductions)[0]
  File "/usr/lib/python1.5/site-packages/kjParser.py", line 1167, in DoParse
    ParseResult = ParseOb.GO()
  File "/usr/lib/python1.5/site-packages/kjParser.py", line 832, in GO
    self.DoOneReduction()
  File "/usr/lib/python1.5/site-packages/kjParser.py", line 725, in DoOneReduction
    tokenVal = Stream.getmember()
  File "/usr/lib/python1.5/site-packages/kjParser.py", line 562, in getmember
    (Token,skip) = self.LexDict.Token(self.String, self.Position)
  File "/usr/lib/python1.5/site-packages/kjParser.py", line 427, in Token
    raise LexTokenError, "Lexical token not found "+info
LexTokenError: Lexical token not found near ::
"t) values ('2',"*"'this is\012multip"

-- 
"So long as the power-seekers clung to the basic premises of the
welfare state, holding need as the criterion of rewards, logic forced
them, step by step, to champion the interests of the less & less
productive... until they reached the ultimate dead end of turning from
the role of champions of 'honest toil' to the role of champions of
open parasitism, parasitism on principle, parasitism as a 'right'."
		-- Ayn Rand "A PreView" _The Ayn Rand Letter_
		   Rick Pasotto email: rickp@telocity.com