python-db-api

Using Python quick insert many columns into Sqlite\\Mysql

帅比萌擦擦* 提交于 2019-12-06 07:22:00
If Newdata is list of x columns, How would get the number unique columns--number of members of first tuple. (Len is not important.) Change the number of "?" to match columns and insert using the statement below. csr = con.cursor() csr.execute('Truncate table test.data') csr.executemany('INSERT INTO test.data VALUES (?,?,?,?)', Newdata) con.commit() By "Newdata is list of x columns", I imagine you mean x tuples , since then you continue to speak of "the first tuple". If Newdata is a list of tuples, y = len(Newdata[0]) is the number of items in the first one of those tuples. Assuming that's the

IronPython db-api 2.0

十年热恋 提交于 2019-12-06 05:17:39
Does anyone know which if any db-api 2.0 drivers work with IronPython? If so, has anyone tried using it with SQLAlchemy, SQLObject or the Django ORM? I know this is a very late answer, but I only saw the question today -- so I am answering it today. http://sourceforge.net/projects/adodbapi contains a fully compliant db-api-2 module which works with IronPython. It is restricted to use in Windows, since it uses classic ADO, using COM calls, rather than ADO.NET. [I tried a true .NET version, but it worked very poorly. The fork for it is still there if anyone wants to follow up.] A fork of this

Why connection in Python's DB-API does not have “begin” operation?

瘦欲@ 提交于 2019-12-03 13:10:15
Working with cursors in mysql-python I used to call "BEGIN;", "COMMIT;", and "ROLLBACK;" explicitly as follows: try: cursor.execute("BEGIN;") # some statements cursor.execute("COMMIT;") except: cursor.execute("ROLLBACK;") then, I found out that the underlying connection object has the corresponding methods: try: cursor.connection.begin() # some statements cursor.connection.commit() except: cursor.connection.rollback() Inspecting the DB-API PEP I found out that it does not mention the begin() method for the connection object, even for the extensions. Mysql-python, by the way, throws the

Python, how to check if a result set is empty?

前提是你 提交于 2019-12-03 02:04:01
I have a sql statement that returns no hits. For example, 'select * from TAB where 1 = 2' . I want to check how many rows are returned, cursor.execute(query_sql) rs = cursor.fetchall() Here I get already exception: "(0, 'No result set')" How can I prevend this exception, check whether the result set is empty? cursor.rowcount will usually be set to 0. If, however, you are running a statement that would never return a result set (such as INSERT without RETURNING , or SELECT ... INTO ), then you do not need to call .fetchall() ; there won't be a result set for such statements. Calling .execute()

Python pysqlite not accepting my qmark parameterization

瘦欲@ 提交于 2019-12-02 09:45:36
问题 I think I am being a bonehead, maybe not importing the right package, but when I do... 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 This

Python pysqlite not accepting my qmark parameterization

为君一笑 提交于 2019-12-02 05:32:40
I think I am being a bonehead, maybe not importing the right package, but when I do... 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 This makes very little sense to me, as the docs show that pysqlite is qmark parametrized. I am new to python

How do you change the SQL isolation level from Python using MySQLdb?

不问归期 提交于 2019-12-01 15:54:14
The documentation I've run across researching this indicates that the way to do it for other databases is to use multiple statements in your query, a la: >>> cursor = connection.cursor() >>> cursor.execute("set session transaction isolation level read uncommitted; select stuff from table; set session transaction isolation level repeatable read;") Unfortunately, doing that yields no results, as apparently the Python DB API (or maybe just this implementation of it?) doesn't support multiple recordsets within a single query. Has anyone else had success with this in the past? I don't think this

How do you change the SQL isolation level from Python using MySQLdb?

a 夏天 提交于 2019-12-01 14:50:49
问题 The documentation I've run across researching this indicates that the way to do it for other databases is to use multiple statements in your query, a la: >>> cursor = connection.cursor() >>> cursor.execute("set session transaction isolation level read uncommitted; select stuff from table; set session transaction isolation level repeatable read;") Unfortunately, doing that yields no results, as apparently the Python DB API (or maybe just this implementation of it?) doesn't support multiple

Inserting multiple rows using psycopg2

主宰稳场 提交于 2019-11-30 21:17:26
问题 According to psycopg2: insert multiple rows with one query, it is much more efficient to use psycopg2's execute instead of executemany . Can others confirm? The above StackOverflow question suggests using mogrify for creating statements of the sort: INSERT INTO table VALUES (value1, value2), (value3, value4) Is it possible to generate such a statement using the regular execute function? I thought something of the form cursor.execute("""INSERT INTO table VALUES (%s, %s), (%s, %s)""", ((value1

difference between cursor and connection objects

情到浓时终转凉″ 提交于 2019-11-30 08:12:54
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. 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 sharing the same connection to a database. Closing the cursor should free resources associated to the