在python中使用sqlite3模块时,除列名之外的cursor.description的所有元素都设置为None,因此该元组不能用于查找查询结果的列类型(与其他符合DB-API的模块不同).获取列的类型pragma table_info(table_name).fetchall()以获取表的描述,将其存储在内存中,然后将cursor.description中的列名与整个表描述相匹配是唯一的方法吗?
我有一个基本程序,应该查询包含用户信息的数据库.我正在尝试为特定用户选择信息并将其打印到控制台.
这是我的代码:
import mysql.connector
funcon = mysql.connector.connect(user='root', password='pass', host='127.0.0.1', database='fundata')
funcursor = funcon.cursor()
query = ("SELECT * FROM funtable WHERE userName=%s")
uName = 'user1'
funcursor.execute(query, uName)
for (userName) in funcursor:
print("{}".format(userName))
Run Code Online (Sandbox Code Playgroud)
我将用户名存储在变量中,因为稍后我计划从tkinter输入框中获取用户名.当我执行此代码时,我收到以下错误:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1
Run Code Online (Sandbox Code Playgroud)
我已经尝试将%s放在查询中的引号中,但随后它逐字地搜索用户名'%s'并且不返回任何内容.如何更改我的代码,以便只查询该用户的数据库?
谢谢.
仅供参考:我使用的是python 3.3.
如何使用pyscopg2(python Postgres驱动程序)取消执行查询语句?
举个例子,假设我有以下代码:
import psycopg2
cnx_string = "something_appropriate"
conn = psycopg2.connect(cnx_string)
cur = conn.cursor()
cur.execute("long_running_query")
Run Code Online (Sandbox Code Playgroud)
然后我想取消从另一个线程执行那个长时间运行的查询 - 我必须在连接/游标对象上调用哪个方法来执行此操作?
在 SQL 语句中通过 %s 获得元组(名称)的正确方法是什么?
names = ('David', 'Isaac')
sql = 'SELECT * from names WHERE name IN %s'
cur.execute(sql,(names,))
Run Code Online (Sandbox Code Playgroud)
/sf/answers/1968236091/中的响应适用于 psycopg2,但不适用于 pg8000。
谢谢!
如果 Newdata 是 x 列的列表,如何获取唯一列数——第一个元组的成员数。(Len 不重要。)更改“?”的数量。匹配列并使用下面的语句插入。
csr = con.cursor()
csr.execute('Truncate table test.data')
csr.executemany('INSERT INTO test.data VALUES (?,?,?,?)', Newdata)
con.commit()
Run Code Online (Sandbox Code Playgroud) 感谢您阅读本文.
我正在开发一个涉及与python进行数据库同步的项目.远程机器是一台带有MySQL的linux机器.客户端不希望我安装除项目所需的python和python库之外的任何东西.虽然它是远程机器,但是可以在不安装MySQL的情况下连接到MySQL.
或者建议我一些解决方法.我不介意保留可执行文件,但我不想安装任何东西,一些解决方法.如果库像MySQLdb一样工作,我会喜欢它.
PEP 249——Python 数据库 API 规范 v2.0在.commit()的描述中指出:
请注意,如果数据库支持自动提交功能,则必须首先关闭该功能。可以提供接口方法来将其重新打开。
鉴于大多数数据库默认为自动提交,其背后的理由是什么?
我正在尝试使用python函数将某些数据库字段更新为SQLite DB。我不断收到以下错误:
ValueError:操作参数必须为str
下面是我的代码。我很想知道如何在sqlite数据库中更新多个列。
def updateEventData():
ID = input('Enter ID of row you\'d like to update: ')
eventname = input('\nPlease enter event name: ')
startdate = input('\nPlease enter event start date: (DD/MM/YYYY): ')
enddate = input('\nPlease enter event end date: (DD/MM/YYYY): ')
venue = input('\nPlease enter event venue: ')
# Sql update
sql = """
UPDATE event_details
SET name, startdate, enddate, venue, (?, ?, ?, ?)
WHERE ROWID = ?
""", (eventname, startdate, enddate, venue, ID)
c.execute(sql)
conn.commit()
Run Code Online (Sandbox Code Playgroud) 我认为我是一个傻瓜,也许不是进口正确的包装,但是当我做...
from pysqlite2 import dbapi2 as sqlite
import types
import re
import sys
...
def create_asgn(self):
stmt = "CREATE TABLE ? (login CHAR(8) PRIMARY KEY NOT NULL, grade INTEGER NOT NULL)"
stmt2 = "insert into asgn values ('?', ?)"
self.cursor.execute(stmt, (sys.argv[2],))
self.cursor.execute(stmt2, [sys.argv[2], sys.argv[3]])
...
I get the error pysqlite2.dbapi2.OperationalError: near "?": syntax error
这对我来说没什么意义,因为文档显示pysqlite是qmark参数化的.我是python和db-api的新手,帮助我!谢谢
我正在使用PyMySQL-0.5.0,并且在将数据从文件加载到远程MySQL实例时遇到晦涩的错误/异常。在执行“LOAD DATA LOCAL INFILE ...”语句中,我看到上面写着一个例外:The used command is not allowed with this MySQL version。
任何提示,如果PyMySQL完全支持此操作(和/或其他一些版本支持)
PS:
1)错误详细信息:
2012-05-17 11:05:24,524 - 8988 - DEBUG - Loading table table_2012_05_16
from file: /path/to/data/file_2012-05-16
2012-05-17 11:05:24,524 - 8988 - DEBUG - Executing update: load data local
infile '/path/to/data/file_2012-05-16' into table table_2012_05_16(@dummy, c1,
c2, c3, c4, c5, c6, c7);
2012-05-17 11:05:24,528 - 8988 - ERROR - Exception while executing update:
<class 'pymysql.err.InternalError'> - (1148, u'The used command is not allowed
with …Run Code Online (Sandbox Code Playgroud) 根据psycopg2:使用一个查询插入多行,使用psycopg2的execute而不是executemany更有效.别人可以证实吗?
上面的StackOverflow问题建议使用mogrify来创建排序语句:
INSERT INTO table VALUES (value1, value2), (value3, value4)
Run Code Online (Sandbox Code Playgroud)
是否可以使用常规执行函数生成这样的语句?我想到了一些形式
cursor.execute("""INSERT INTO table VALUES (%s, %s), (%s, %s)""", ((value1,value2),(value3,value4)))
Run Code Online (Sandbox Code Playgroud)
会工作.
更新:
例如,我尝试传入执行sql语句:
insert into history (timestamp) values (%s),(%s);
Run Code Online (Sandbox Code Playgroud)
与下面的元组:
(('2014-04-27 14:07:30.000000',), ('2014-04-27 14:07:35.000000',))
Run Code Online (Sandbox Code Playgroud)
但我得到的只是错误:
没有结果要取
python ×11
python-db-api ×11
sqlite ×4
mysql ×2
mysql-python ×2
postgresql ×2
psycopg2 ×2
pg8000 ×1
pysqlite ×1
sql-update ×1
transactions ×1
where-in ×1