mysql-python

how to compare the rows of a table using python

强颜欢笑 提交于 2019-12-11 11:55:17
问题 Sorry if the title was misleading or not very descriptive, but here is what I want I have a table with 2 columns : name|value 1.aaaa|132412 2.aaaa|234124 3.aaaa|542253 bbbb|234324 bbbb|342342 so I want to basically compare the rows where name="aaaa" the part where I am stuck is how do I compare all the rows with name="aaaa" sql="select value from table where name='aaaa'" cursor.execute(sql) result=cursor.fetchall() for row in result: value=row[0] how do I go about from here ? edit: I want to

Python MYSQLdb documentation missing details?

末鹿安然 提交于 2019-12-11 11:51:05
问题 I am trying to make sense of MySQLdb documentation. I was just wondering if there are things missing from there. For example, I am trying to see what "rowcount" (a constant) actually does, but am not seeing it anywhere in the documentation. So is the documentation incomplete or am I just looking at the wrong place? Thanks. 回答1: The primary source of documentation for Python database modules is the DB-API 2.0 specification: .rowcount This read-only attribute specifies the number of rows that

Python : What's wrong with my code of multi processes inserting to MySQL?

主宰稳场 提交于 2019-12-11 07:48:43
问题 I've write a Python script to insert some data(300 millions) to a MySQL table: #!/usr/bin/python import os import MySQLdb from multiprocessing import Pool class DB(object): def __init__(self): self.conn = MySQLdb.connect(host='localhost',user='root',passwd='xxx',db='xdd',port=3306) self.cur = self.conn.cursor() def insert(self, arr): self.cur.execute('insert into RAW_DATA values(null,%s,%s,%s,%s,%s,%s,%s)', arr) def close(self): self.conn.commit() self.cur.close() self.conn.close() def Import

Django DB transactions and deadlocks

三世轮回 提交于 2019-12-11 07:37:17
问题 When running Django with Gunicorn with multiple processes/workers I'm getting a deadlock issue with some of my manual MySQL database transactions. DatabaseError(1205, 'Lock wait timeout exceeded; try restarting transaction') My setup uses multiple databases, and my function needs to be passed the database to use when it's called. For this reason, I can't use the standard Django transaction decorators as the db needs to be hard-coded as an argument. I've inspected the decorator code to look at

MySQL: How to update remote webserver with differential data

淺唱寂寞╮ 提交于 2019-12-11 07:32:40
问题 On my local machine, I maintain several InnoDB tables which are mirror replica of the ones on my web server. Whenever I have new or updated data, I perform that on the local tables. For the updates, it's typically to only a few fields of some tables. Doing locally helps me to perform processing and validation of the data before staging. Finally, I want to move these differential data to the remote server. How can I best achieve this whole process of data migration. There are a few stages

Problem installing MySQL-Python on Mac OS 10.14.1

最后都变了- 提交于 2019-12-11 06:34:09
问题 I recently upgraded to Mac OS Mojave and now can't install MySQL-Python: pip install MySQL-Python ... clang -bundle -undefined dynamic_lookup build/temp.macosx-10.13-x86_64-2.7/_mysql.o -L/usr/local/opt/mysql-client/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.13-x86_64-2.7/_mysql.so ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command 'clang' failed with exit status 1 The stack trace says that the "ssl"

MySQLdb: Operand should contain 1 column(s)

帅比萌擦擦* 提交于 2019-12-11 05:49:07
问题 I am trying to insert some data into a MySQL database, using Python and MySQLdb. When I execute the following function in my program, MySQL returns error "1241, 'Operand should contain 1 column(s)'" User, password and database are correct, the table is existing and all rights are granted. def write_to_mysql(pname, dat, like, reachs, talker, friendsfans): '' try: con = mdb.connect(user='user', passwd='password', host='localhost', db='database'); except Exception. err: print(err) with con: cur

Python MySQLdb: Iterating over a cursor

孤街醉人 提交于 2019-12-11 04:48:21
问题 In another post, this code: connection = MySQLdb.connect(...) cursor = connection.cursor() cursor.execute("SHOW TABLES") for (table_name,) in cursor: print(table_name) correctly iterates over the table names in the cursor whereas this code: for table_name in cursor: print(table_name) returns elements in the form: ('some_table',) After much searching, I have been unable to make sense of this. Could someone explain the difference? I cannot figure out exactly what execute() is returning. Also, I

Exporting pandas dataframe to MySQL using SQLAlchemy

淺唱寂寞╮ 提交于 2019-12-11 04:47:45
问题 I intend to export a pandas dataframe to MySQL using SQLAlchemy. Despite referring to all previous posts, I am unable to solve the issue: import pandas as pd import pymysql from sqlalchemy import create_engine df=pd.read_excel(r"C:\Users\mazin\1-601.xlsx") cnx = create_engine('mysql+pymysql://[root]:[aUtO1115]@[localhost]:[3306]/[patenting in psis]', echo=False) df.to_sql(name='inventor_dataset', con=cnx, if_exists = 'replace', index=False) Following is the error: OperationalError: (pymysql

Mysqldb AttributeError: cursor

余生长醉 提交于 2019-12-11 03:32:41
问题 I am starting to use the mysqldb module in python and I seem to have some issues with the "standard" way of calling queries. I understand that the standard way is to create a cursor and then use it to execute queries. However, when I try to instanciate one, it gives me the following error : AttributeError: cursor My Database class looks like : class Database(): def __init__(self): server = "localhost" login = "login" password = "passws" database = "DB" my_conv = { FIELD_TYPE.LONG: int } self