python mysql error in query -


i want generate dynamic table:

columnames=[element[0] element in bufferdata['data'] ] index,element in enumerate(columnames):         columnames[index]=re.sub("[(%./)-]","",element)     tuple(columnames)     querycreatetable='''create table test (id int auto_increment,name varchar(50),symbol varchar(10),sector varchar(50),                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float,%s float,%s float,                                            %s float,%s float,%s float                                            )                          '''     try:         self.cursor.execute(querycreatetable,columnames)     except mysqldb.programmingerror, e:         try:             print "mysql error [%d]: %s" % (e.args[0], e.args[1])         except indexerror:             print "mysql error: %s" % str(e) 

but receive error: mysql error [1064]: have error in sql syntax; check manual corresponds mysql server version right syntax use near ''sales in millions' float,'earnings per share' float,'pe ratio ttm' float,'pe hi' @ line 2

does see problem is?

firstly, told here : check valid sql column name

sql identifiers , key words must begin letter (a-z, letters diacritical marks , non-latin letters) or underscore (_). subsequent characters in identifier or key word can letters, underscores, digits (0-9), or dollar signs ($). note dollar signs not allowed in identifiers according letter of sql standard, use might render applications less portable

it comes postgre doc, because postgre close "ideal" sql syntax, might same mysql... no parenthesis column names, no spaces...

and secondly, column names not strings :

the following syntax valid:

create table (test varchar(100) not null, ...) 

and following 1 invalid , throw syntax error:

create table ('test' varchar(100) not null, ...) 

when use '%s' modifier, parses data string. surrounds quotes, invalid...

so create table, suggest "for loop" validate data (with regexpr), , symply add string:

import re # ... query = "create table test (id int auto_increment,name varchar(50)" c in columnames:         if (re.search(r"^[a-za-z][a-za-z0-9_]*$", c) query += c + ", float" #this regex validate string if  begins alphabetic char (upper or lower case), , if others characters alphanumeric, or underscores         else raise syntaxerror("invalid column name!!") #if not, raise syntax error query += ");" 

and can create table :)


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -