728x90
반응형
Python
Using the Python DB API, don't do this:
# Do NOT do it this way.
cmd = "update people set name='%s' where id='%s'" % (name, id)
curs.execute(cmd)
Instead, do this:
cmd = "update people set name=%s where id=%s"
curs.execute(cmd, (name, id))
Note that the placeholder syntax depends on the database you are using.
'qmark' Question mark style,
e.g. '...WHERE name=?'
'numeric' Numeric, positional style,
e.g. '...WHERE name=:1'
'named' Named style,
e.g. '...WHERE name=:name'
'format' ANSI C printf format codes,
e.g. '...WHERE name=%s'
'pyformat' Python extended format codes,
e.g. '...WHERE name=%(name)s'
The values for the most common databases are:
>>> import MySQLdb; print MySQLdb.paramstyle
format
>>> import psycopg2; print psycopg2.paramstyle
pyformat
>>> import sqlite3; print sqlite3.paramstyle
qmark
So if you are using MySQL or PostgreSQL, use %s
(even for numbers and other non-string values!) and if you are using SQLite use ?
728x90
반응형
'개발 > Python' 카테고리의 다른 글
[Python3] konlpy 설치시 jpype 관련 실패 해결 방안 (0) | 2017.09.01 |
---|---|
R에서 파이썬까지…데이터과학 학습 사이트 8 (0) | 2017.05.15 |
mysqldb (0) | 2016.09.28 |
libSM.so.6 (0) | 2016.05.22 |
MIT 6.00 컴퓨터 공학과 프로그래밍(Python) 오픈 코스 (0) | 2016.05.15 |