print function in python3.1

  • Thread starter Anjanesh Lekshminarayanan
  • Start date
A

Anjanesh Lekshminarayanan

Python 3.1.1

sql = "INSERT INTO `tbl` VALUES (NULL, '%s', '%s', '%s', '%s', '%s');"
for row in fp:
print (sql, (row[0],row[1],row[2],row[3],row[4]))
..
INSERT INTO `tbl` VALUES (NULL, '%s', '%s', '%s', '%s', '%s'); ('142',
'abc', '2006-04-09 02:19:24', '', '')
..
Why is it showing %s in the output ?

1. I dont want to sql % () because that doesnt escape the strings
2. I cant use conn.escape_string(r) because Im not connected to a
database. Output script to file and import to database loated
elsewhere.
 
D

Diez B. Roggisch

Anjanesh said:
Python 3.1.1

sql = "INSERT INTO `tbl` VALUES (NULL, '%s', '%s', '%s', '%s', '%s');"
for row in fp:
print (sql, (row[0],row[1],row[2],row[3],row[4]))
.
INSERT INTO `tbl` VALUES (NULL, '%s', '%s', '%s', '%s', '%s'); ('142',
'abc', '2006-04-09 02:19:24', '', '')
.
Why is it showing %s in the output ?

Because you gave it to it. How should it know that you want the %s to be
replaced with the other parameters? That's what the %-operator is for.
1. I dont want to sql % () because that doesnt escape the strings
2. I cant use conn.escape_string(r) because Im not connected to a
database. Output script to file and import to database loated
elsewhere.

Depending on your DB-adapter, you are out of luck here. Either connect to a
db even if you don't need it, or try & see if you can locate the
implementation in the module somehow.

E.g. MySQLdb has the function exposed as "escape_string", not only on a
connection.

Diez
 
A

Anjanesh Lekshminarayanan

Depending on your DB-adapter, you are out of luck here. Either connect to a db even if you don't need it, or try & see if you can locate the implementation in the module somehow.

ImportError: No module named MySQLdb
MySQLdb only available in Python2.
 
D

Diez B. Roggisch

Anjanesh said:
ImportError: No module named MySQLdb
MySQLdb only available in Python2.

I don't have the slightest clue what you want to say with that.

Diez
 
A

Anjanesh Lekshminarayanan

As of now, there is no mysql adaptor for Python3. Hence cant use escape_string()
 
C

Carsten Haese

Anjanesh said:
As of now, there is no mysql adaptor for Python3. Hence cant use escape_string()

Maybe it would help if you explained what you are actually trying to
accomplish.
 
A

Anjanesh Lekshminarayanan

Maybe it would help if you explained what you are actually trying to
accomplish.

import csv
f = csv.reader(open('data.txt'), delimiter='\t') # 2GB text file
sql = "INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s);"
for row in f:
print (sql, (row[0],row[1],row[2],row[3],row[4]))

$ python3 parse.py3 > data.sql

But because of print() being a function in Py3,
print (sql, (row[0],row[1],row[2],row[3],row[4]))
prints
INSERT INTO `data` VALUES (NULL, '%s', '%s', '%s', '%s', '%s');
('142', 'abcde', '2006-03-01 05:17:14', '', '')
instead of
INSERT INTO `data` VALUES (NULL, '142', 'abcde', '2006-03-01 05:17:14', '', '');

Anjanesh
 
D

Diez B. Roggisch

Anjanesh said:
Maybe it would help if you explained what you are actually trying to
accomplish.

import csv
f = csv.reader(open('data.txt'), delimiter='\t') # 2GB text file
sql = "INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s);"
for row in f:
print (sql, (row[0],row[1],row[2],row[3],row[4]))

$ python3 parse.py3 > data.sql

But because of print() being a function in Py3,
print (sql, (row[0],row[1],row[2],row[3],row[4]))
prints
INSERT INTO `data` VALUES (NULL, '%s', '%s', '%s', '%s', '%s');
('142', 'abcde', '2006-03-01 05:17:14', '', '')
instead of
INSERT INTO `data` VALUES (NULL, '142', 'abcde', '2006-03-01 05:17:14',
'', '');

No. It does so because you don't use

print(sql % (row[0],row[1],row[2],row[3],row[4]))

And that has nothing to do with print being a function or not.

Diez
 
D

Dennis Lee Bieber

Maybe it would help if you explained what you are actually trying to
accomplish.

import csv
f = csv.reader(open('data.txt'), delimiter='\t') # 2GB text file
sql = "INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s);"
for row in f:
print (sql, (row[0],row[1],row[2],row[3],row[4]))

$ python3 parse.py3 > data.sql

But because of print() being a function in Py3,

Nothing to do with print()... Python 2.x would do the same thing...

PythonWin 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit
(Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin'
for further copyright information.
row = tuple(range(5))
row (0, 1, 2, 3, 4)
sql = "INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s);"
print sql, (row[0],row[1],row[2],row[3],row[4])
INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s); (0, 1, 2, 3, 4)

vs
print sql % (row[0],row[1],row[2],row[3],row[4]) INSERT INTO `data` VALUES (NULL,0,1,2,3,4);

There is nothing magic about %s -- it is just a placeholder used by
operations that work on strings (and, since the MySQLdb adapter sends
strings, using Python's % operator, it is the placeholder also used by
the adapter).

You appear to be building a batch file of SQL insert statements...
What is your target database engine? OR: must you use Python 3.x? After
all -- for your apparent needs I wouldn't consider 3.x to be "production
ready".

Does Python 3.x include SQLite? It may be simpler to just use it to
create a scratch database, then use the command line program to dump the
scratch data as SQL (might need to post process to remove unwanted
statements).

E:\UserData\Dennis Lee Bieber\My Documents>sqlite3 test.db
SQLite version 3.6.3
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .dump test
BEGIN TRANSACTION;
CREATE TABLE test ( id primary key, stuff char );
INSERT INTO "test" VALUES(NULL,'one');
INSERT INTO "test" VALUES(NULL,'more');
INSERT INTO "test" VALUES(NULL,'one');
INSERT INTO "test" VALUES(NULL,'more');
INSERT INTO "test" VALUES(NULL,'test');
COMMIT;
sqlite> .dump stats
BEGIN TRANSACTION;
CREATE TABLE stats (id integer primary key,
name char(50),
num numeric);
INSERT INTO "stats" VALUES(1,'integer range',123);
INSERT INTO "stats" VALUES(2,'single range',3.14159265);
INSERT INTO "stats" VALUES(3,'single range int',31415926500);
INSERT INTO "stats" VALUES(4,'double range',5.12345676890123);
INSERT INTO "stats" VALUES(5,'double range int?',5.12345676890124e+20);
COMMIT;
sqlite>

Otherwise, you are going to have write the equivalent function as
MySQLdb's escape_strings(), which replaces dangerous characters (oh, it
also converts all data types to string first) with escaped versions --
and THEN wraps the string with the proper quotes.
 
T

Terry Reedy

Dennis said:
Does Python 3.x include SQLite?
Of course ;-]
['Binary', 'Cache', 'Connection', 'Cursor', 'DataError',
[snip]
adapt', 'adapters', 'apilevel', 'complete_statement', 'connect',
'converters', 'datetime', 'dbapi2', 'enable_callback_tracebacks',
'enable_shared_cache', 'paramstyle', 'register_adapter',
'register_converter', 'sqlite_version', 'sqlite_version_info',
'threadsafety', 'time', 'version', 'version_info']

but is does not seem to have exposed escape_string function
 
D

Dennis Lee Bieber

but is does not seem to have exposed escape_string function

SQLite uses "prepared statements" in the engine end, sending
parameters in a separate data block -- hence it doesn't need to escape
them on input. MySQL, however, didn't have prepared statements until
version 5, but MySQLdb is still version 4 (maybe even 3) compatible --
it sends everything as complete string statements, so needs to escape
parameters when creating a complete string.

If you reread my prior response, I suggested using SQLite to create
a scratch database -- ie, actually execute the inserts. THEN use the
SQLite command line utility to do a
.output somefile.sql
.dump scratchtable
.output stdout

With a little clean up (one probably doesn't want the create table
statements) the result should be a viable file of SQL commands which
could work with whatever the end database engine is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top