mysql-python

How to run SQLAlchemy on AWS Lambda in Python

你离开我真会死。 提交于 2019-12-11 03:19:13
问题 I preapre very simple file for connecting to external MySQL database server, like below: from sqlalchemy import * def run(event, context): sql = create_engine('mysql://root:root@127.0.0.1/scraper?charset=utf8'); metadata = MetaData(sql) print(sql.execute('SHOW TABLES').fetchall()) Doesn't work on AWS, but localy on Windows works perfectly. Next, I install by pip install sqlalchemy --target my/dir and prepare ZIP file to upload packages to AWS Lambda. Run, but with failed message No module

Python MySQLDB Insert with variables as parameters

别等时光非礼了梦想. 提交于 2019-12-11 03:08:35
问题 I am using Python-MySQLdB library. I am trying to connect to the MySQL DB from a python script. I have a requirement where when the users register their account automatically the details should be added/saved to the MySQL DB. I'm trying to automate this feature using python. The script is running successfully and the values are not reflected in the DB. What might be the issue ?? This is the script. import sys import MySQLdb as mdb import datetime username ='trail' password='trial' companyname

Python MySQL parameter queries for dynamic table names

隐身守侯 提交于 2019-12-11 02:38:24
问题 I want to query MySQL tables using my python program. However, the tables that I want to query are the ones whose names are passed in as variables (parameters). I tried something like this: readTable_query = """SELECT * FROM %s""" readTable_cursor.execute(readTable_query, (table_name)) The problem with this is that the dynamically generated SQL query is putting the table name in quotes, i.e. instead of generating a query like "SELECT * FROM products", its generating a query like "SELECT *

MySQL in Python; connection closes in for loop after first cycle

浪子不回头ぞ 提交于 2019-12-11 00:57:20
问题 My MySQL connection closes after one loop cycle. I probably do something wrong in the usage of the mysql object. It outputs one cycle and then exits with an error. This is my code: cnx = mysql.connector.connect(**config) cursor = cnx.cursor() fetch_ids = cnx.cursor() query = ("SELECT * FROM items WHERE has_recipe = 1") fetch_ids.execute(query) count = 0 for (data) in fetch_ids: fetch_details = cnx.cursor() query = ("SELECT * FROM recipes WHERE recipe_id = " + str(data[1])) fetch_details

Python MySQL executemany in WHERE clause

大憨熊 提交于 2019-12-11 00:10:01
问题 I want to select values from MySQL as follows do_not_select = [1,2,3] cursor = database.cursor() cursor.executemany("""SELECT * FROM table_a WHERE id != %s""",(do_not_select)) data = cursor.fetchall() The query return all the values in the db apart form the first id (1). I don't want it to select id 1,2 or 3 however. Is this possible using the executemany command..? 回答1: Give NOT IN a go: do_not_select = [1, 2, 3] cursor.execute("""SELECT * FROM table_a WHERE id NOT IN ({}, {}, {})""".format

Python: MySQL connection is open, but can't create cursor

╄→гoц情女王★ 提交于 2019-12-11 00:05:30
问题 I'm trying to open a cursor to a MySQL-DB. But I'm getting this error: 'NoneType' object has no attribute 'cursor' Here is a small sourcecode: class Sample: def __init__(self): self.conn = None self.value = self.setValue() def connect(self): self.conn = MySQLdb.connect(...) #cursor = self.conn.cursor() #cursor.execute("SELECT ...") #value = str(cursor.fetchone()[0]) #raise Exception(value) #cursor.close() <- here everything is working fine def setValue(self): if (self.conn == None): self

Executing MySQL SELECT * query in parallel

那年仲夏 提交于 2019-12-10 22:18:31
问题 I have a multithreaded application that periodically fetches the whole content of the MySQL table (with SELECT * FROM query) The application is written in python, uses threading module to multithreading and uses mysql-python (mysqldb) as MySQL driver (using mysqlalchemy as a wrapper produces similar results). I use InnoDB engine for my MySQL database. I wrote a simple test to check the performance of SELECT * query in parallel and discovered that all of those queries are implemented

PyCharm import MySQL

╄→尐↘猪︶ㄣ 提交于 2019-12-10 19:19:27
问题 I am trying import MySQLdb as mdb on PyCharm and get the error: ImportError: No module named MySQLdb From the PyCharm-preference, cannot find MySQLdb to import. pymysql was successfully installed on PyCharm though. Suggestions ? Thank you very much. 回答1: You can use pymysql installing through PyCharm instead of MySQLdb. It's a drop-in replacement for it. Documentation: https://github.com/PyMySQL/PyMySQL 来源: https://stackoverflow.com/questions/39499387/pycharm-import-mysql

cursor.fetchone() returns None but row in the database exists

扶醉桌前 提交于 2019-12-10 17:56:08
问题 I am writing a program on python which interacts with MySQL database. For sql queries I use MySQLdb. The problem is that fetchone() returns None but with the database browser I can see that that row exists. This piece of code: query = "SELECT * FROM revision WHERE rev_id=%s;" cursor.execute(query % revision_id) row = cursor.fetchone() if row == None: raise Exception("there isn't revision with id %s" % revision_id) I have no idea what is going on here. Any ideas? EDIT: okay, in some cases it

How to avoid duplicate entries in a MySQL database without throwing an error

给你一囗甜甜゛ 提交于 2019-12-10 17:19:46
问题 I am using the Python-MySQL (MySQLdb) library to insert values into a database. I want to avoid duplicate entries from being inserted into the database, so I have added the unique constraint to that column in MySQL. I am checking for duplicates in the title column. In my Python script, I am using the following statement: cursor.execute ("""INSERT INTO `database` (title, introduction) VALUES (%s, %s)""", (title, pure_introduction)) Now when a duplicate entry is added to the database, it will