python-db-api

How to prevent PyMySQL from escaping identifier names?

瘦欲@ 提交于 2019-12-24 03:41:18
问题 I am utilizing PyMySQL with Python 2.7 and try to execute the following statement: 'INSERT INTO %s (%s, %s) VALUES (%s, %s) ON DUPLICATE KEY UPDATE %s = %s' With the following parameters: ('artikel', 'REC_ID', 'odoo_created', u'48094', '2014-12-23 10:00:00', 'odoo_modified', '2014-12-23 10:00:00') Always resulting in: {ProgrammingError}(1064, u"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 ''artikel' ('REC

Follow up: Execute .sql files from python

喜欢而已 提交于 2019-12-22 04:46:07
问题 Over a year ago someone asked this question: Execute .sql files that are used to run in SQL Management Studio in python. I am writing a script in python that connects to a SQL server and creates and populates a database based on SQL commands in a large (several GBs) .sql file. It looks like SQLCMD requires a download and install of SQL Server Express. Are there other ways to execute a .sql file from python without requiring everyone who uses my script to download and install SQL Server? Does

Why close a cursor for Sqlite3 in Python

青春壹個敷衍的年華 提交于 2019-12-21 17:41:50
问题 Is there any benefit to closing a cursor when using Python's sqlite3 module? Or is it just an artifact of the DB API v2.0 that might only do something useful for other databases? It makes sense that connection.close() releases resources; however, it is unclear what cursor.close() actually does, whether it actually releases some resource or does nothing. The docs for it are unenlightening: >>> import sqlite3 >>> conn = sqlite3.connect(':memory:') >>> c = conn.cursor() >>> help(c.close) Help on

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

被刻印的时光 ゝ 提交于 2019-12-21 04:21:28
问题 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()

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

瘦欲@ 提交于 2019-12-20 10:59:43
问题 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? 回答1: 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 ),

inserting numpy integer types into sqlite with python3

白昼怎懂夜的黑 提交于 2019-12-18 04:11:26
问题 What is the correct way to insert the values of numpy integer objects into databases in python 3? In python 2.7 numpy numeric datatypes insert cleanly into sqlite, but they don't in python 3 import numpy as np import sqlite3 conn = sqlite3.connect(":memory:") conn.execute("CREATE TABLE foo (id INTEGER NOT NULL, primary key (id))") conn.execute("insert into foo values(?)", (np.int64(100),)) # <-- Fails in 3 The np.float types seem to still work just fine in both 2 and 3. conn.execute("insert

Inserting JSON into MySQL using Python

爷,独闯天下 提交于 2019-12-17 22:16:28
问题 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

Transactions with Python sqlite3

十年热恋 提交于 2019-12-17 15:25:00
问题 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

Do cursors in Django run inside the open transaction?

China☆狼群 提交于 2019-12-10 15:42:54
问题 My Django application is using some custom SQL which I am executing inside a view like this: db = router.db_for_write(model) cursor = connections[db].cursor() cursor.execute("INSERT INTO ....") Since I am using the TransactionMiddleware , my view is running inside a transaction, but I'm not clear if getting a new cursor like this "escapes" the currently open transaction or if the cursor is still a part of the open transaction. I am getting some error messages that lead me to believe that

How to get prepared query which is sent to db

泄露秘密 提交于 2019-12-10 13:32:30
问题 When using database libraries like pyodbc that implement the Python Database API Specification how can you get the fully prepared query after parameter substitution has been applied. I'm making a call to a Sybase stored procedure that will receive 18 arguments through parameter substitution. I would like to capture the actual call that was made and log it to help in debugging. A simpler example of what I need: pyodbc example import pyodbc conn = pyodbc.connect('DSN=test;PWD=password') c =