如何多次查询并最后关闭连接? [英] How to query multiple times and close the connection at the end?

查看:177
本文介绍了如何多次查询并最后关闭连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个与mysql数据库的连接,并使用不同的查询检索数据.每次获取数据时都需要关闭连接吗?还是有更好的方法来多次查询并仅在最后关闭连接?

I'd like to open a connection to mysql database and retrieve data with different queries. Do I need to close the connection every time I fetch the data or is there a better way to query multiple times and close the connection only at the end?

当前,我这样做:

db = dbConnect(MySQL(), user='root', password='1234', dbname='my_db', host='localhost')

query1=dbSendQuery(db, "select * from table1")
data1 = fetch(query1, n=10000)
query2=dbSendQuery(db, "select * from table2")  ##ERROR !

我收到错误消息:

mysqlExecStatement(conn,statement,...)中的错误: RS-DBI驱动程序:(与挂起的行连接,在继续之前先关闭resultSet)

Error in mysqlExecStatement(conn, statement, ...) : RS-DBI driver: (connection with pending rows, close resultSet before continuing)

现在,如果我使用 dbClearResult(query1)清除结果,则需要重做连接(dbConnect ...)

Now if I clear the result with dbClearResult(query1) I need to redo the connection (dbConnect...)

是否有更好/有效的方法来首先获取所有内容,而不是每次都打开/关闭?

Is there a better/efficient way to fetch everything first instead of open/close every time?

推荐答案

尝试使用dbGetQuery(...),而不要像这样使用dbSendQuery(...)fetch()

Try dbGetQuery(...) instead of using dbSendQuery(...) and fetch() like this

db = dbConnect(MySQL(), user='root', password='1234', dbname='my_db', host='localhost')

query1=dbGetQuery(db, "select * from table1")
query2=dbGetQuery(db, "select * from table1")

从帮助页面:

"dbGetQuery"函数可以一次完成所有这些操作(提交语句,获取所有输出记录,并清除结果集).

The function ‘dbGetQuery’ does all these in one operation (submits the statement, fetches all output records, and clears the result set).

这篇关于如何多次查询并最后关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆