sql创建表和数据精度

时间:2014-07-05 20:37:19

标签: python sql

我很抱歉这个简单的问题。但我真的没有发现这个sql语句(python)的错误:

cursor.execute('CREATE TABLE measure_%s (id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ztime int(11), mvalue DOUBLE)', (ptype,))

“mvalue”的值为12.34。这意味着两个小数和0到40之间的值。所以我猜DOUBLE是正确的......

这个有效:

sql = """CREATE TABLE measure_"""+ptype+""" (
 id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
 ztime int(11),
 mvalue FLOAT(5,2) )"""
cursor.execute(sql)

但我仍然没有看到与第一个不同的区别。

1 个答案:

答案 0 :(得分:1)

对值使用DBAPI占位符,但不使用表操作语句。

"CREATE TABLE {}".format('mytable')(Python格式化),

cur.execute("insert into people values (?, ?)", (who, age)) - 数据库引用,了解不同的类型。

http://ianhowson.com/a-quick-guide-to-using-mysql-in-python.html