python-db-api

difference between cursor and connection objects

我们两清 提交于 2019-11-29 06:07:22
问题 I am confused about why python needs cursor object. I know jdbc and there the database connection is quite intuitive but in python I am confused with cursor object. Also I am doubtful about what is the difference between cursor.close() and connection.close() function in terms of resource release. 回答1: The cursor paradigm is not specific to Python but are a frequent data structure in databases themselves. Depending on the underlying implementation it may be possible to generate several cursors

Set database connection timeout in Python

寵の児 提交于 2019-11-29 03:50:54
I'm creating a RESTful API which needs to access the database. I'm using Restish, Oracle, and SQLAlchemy. However, I'll try to frame my question as generically as possible, without taking Restish or other web APIs into account. I would like to be able to set a timeout for a connection executing a query. This is to ensure that long running queries are abandoned, and the connection discarded (or recycled). This query timeout can be a global value, meaning, I don't need to change it per query or connection creation. Given the following code: import cx_Oracle import sqlalchemy.pool as pool conn

Inserting JSON into MySQL using Python

岁酱吖の 提交于 2019-11-28 17:59:15
I have a JSON object in Python. I am Using Python DB-API and SimpleJson. I am trying to insert the json into a MySQL table. At moment am getting errors and I believe it is due to the single quotes '' in the JSON Objects. How can I insert my JSON Object into MySQL using Python? Here is the error message I get: error: uncaptured python exception, closing channel <twitstream.twitasync.TwitterStreamPOST connected at 0x7ff68f91d7e8> (<class '_mysql_exceptions.ProgrammingError'>: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the

Python db-api: fetchone vs fetchmany vs fetchall

百般思念 提交于 2019-11-28 16:18:32
I just had a discussion today with some coworkers about python's db-api fetchone vs fetchmany vs fetchall. I'm sure the use case for each of these is dependent on the implementation of the db-api that I'm using, but in general what are the use cases for fetchone vs fetchmany vs fetchall? In other words are the following equivalent? or is there one of these that is preferred over the others? and if so in which situations? cursor.execute("SELECT id, name FROM `table`") for i in xrange(cursor.rowcount): id, name = cursor.fetchone() print id, name cursor.execute("SELECT id, name FROM `table`")

Transactions with Python sqlite3

戏子无情 提交于 2019-11-28 04:08:32
I'm trying to port some code to Python that uses sqlite databases, and I'm trying to get transactions to work, and I'm getting really confused. I'm really confused by this; I've used sqlite a lot in other languages, because it's great, but I simply cannot work out what's wrong here. Here is the schema for my test database (to be fed into the sqlite3 command line tool). BEGIN TRANSACTION; CREATE TABLE test (i integer); INSERT INTO "test" VALUES(99); COMMIT; Here is a test program. import sqlite3 sql = sqlite3.connect("test.db") with sql: c = sql.cursor() c.executescript(""" update test set i =

how to safely generate a SQL LIKE statement using python db-api

守給你的承諾、 提交于 2019-11-27 22:54:38
I am trying to assemble the following SQL statement using python's db-api: SELECT x FROM myTable WHERE x LIKE 'BEGINNING_OF_STRING%'; where BEGINNING_OF_STRING should be a python var to be safely filled in through the DB-API. I tried beginningOfString = 'abc' cursor.execute('SELECT x FROM myTable WHERE x LIKE '%s%', beginningOfString) cursor.execute('SELECT x FROM myTable WHERE x LIKE '%s%%', beginningOfString) I am out of ideas; what is the correct way to do this? It's best to separate the parameters from the sql if you can. Then you can let the db module take care of proper quoting of the

Python db-api: fetchone vs fetchmany vs fetchall

蓝咒 提交于 2019-11-27 19:52:29
问题 I just had a discussion today with some coworkers about python's db-api fetchone vs fetchmany vs fetchall. I'm sure the use case for each of these is dependent on the implementation of the db-api that I'm using, but in general what are the use cases for fetchone vs fetchmany vs fetchall? In other words are the following equivalent? or is there one of these that is preferred over the others? and if so in which situations? cursor.execute("SELECT id, name FROM `table`") for i in xrange(cursor

Set database connection timeout in Python

被刻印的时光 ゝ 提交于 2019-11-27 17:54:59
问题 I'm creating a RESTful API which needs to access the database. I'm using Restish, Oracle, and SQLAlchemy. However, I'll try to frame my question as generically as possible, without taking Restish or other web APIs into account. I would like to be able to set a timeout for a connection executing a query. This is to ensure that long running queries are abandoned, and the connection discarded (or recycled). This query timeout can be a global value, meaning, I don't need to change it per query or

Escape SQL “LIKE” value for Postgres with psycopg2

人走茶凉 提交于 2019-11-27 12:22:00
Does psycopg2 have a function for escaping the value of a LIKE operand for Postgres? For example I may want to match strings that start with the string "20% of all", so I want to write something like this: sql = '... WHERE ... LIKE %(myvalue)s' cursor.fetchall(sql, { 'myvalue': escape_sql_like('20% of all') + '%' } Is there an existing escape_sql_like function that I could plug in here? (Similar question to How to quote a string value explicitly (Python DB API/Psycopg2) , but I couldn't find an answer there.) Yeah, this is a real mess. Both MySQL and PostgreSQL use backslash-escapes for this

how to safely generate a SQL LIKE statement using python db-api

不问归期 提交于 2019-11-26 21:13:59
问题 I am trying to assemble the following SQL statement using python's db-api: SELECT x FROM myTable WHERE x LIKE 'BEGINNING_OF_STRING%'; where BEGINNING_OF_STRING should be a python var to be safely filled in through the DB-API. I tried beginningOfString = 'abc' cursor.execute('SELECT x FROM myTable WHERE x LIKE '%s%', beginningOfString) cursor.execute('SELECT x FROM myTable WHERE x LIKE '%s%%', beginningOfString) I am out of ideas; what is the correct way to do this? 回答1: It's best to separate