python操作MySQL数据库
MySQLdb呢,其实和Python内置的sqlite3的使用方法基本相同。 警告: 不要使用字符串拼接生成SQL语句,否则可能产生SQL注入的问题。应当使用 execute() 的第二个参数检查输入的合法性。 #do NOT do this! cmd = "update people set name='%s' where id='%s'" % (name, id) cur.execute(cmd) # instead, do this: cmd = "update people set name=%s where id=%s" cur.execute(cmd, (name, id)) 采用的是MySQLdb操作的MYSQL数据库。先来一个简单的例子吧: import MySQLdb try: conn=MySQLdb.connect(host='localhost',user='root',passwd='mysql',db='test',port=3306) cur=conn.cursor() cur.execute('select * from user') cur.close() conn.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])