mysql-python

Populate a dictionary with the result of a query

此生再无相见时 提交于 2020-01-04 05:31:28
问题 I'm currently doing this: cursor.execute('SELECT thing_id, thing_name FROM things') things = []; for row in cursor.fetchall(): things.append(dict([('thing_id',row[0]), ('thing_name',row[1]) ])) Is there some shorthand I can use to do this, or should I write a little helper function? 回答1: Using list comprehension: things = [{'thing_id': row[0], 'thing_name': row[1]} for row in cursor.fetchall()] or using list comprehension with zip: things = [dict(zip(['thing_id', 'thing_name'], row)) for row

mysql-python on mac osx 10.9.2: error: command '/usr/bin/clang' failed with exit status 1

↘锁芯ラ 提交于 2020-01-03 08:22:08
问题 I wanted to port my django app from sqlite to mysql. But when I'm trying to install mysql-python, it gives me this error: error: command '/usr/bin/clang' failed with exit status 1 I looked around for clues and tried this solution which seemed to have worked for most people: sudo su export CFLAGS=-Qunused-arguments export CPPFLAGS=-Qunused-arguments pip install MySQL-python The complete error log is here: pip install MySQL-python Downloading/unpacking MySQL-python Running setup.py egg_info for

Getting “Library not loaded: libssl.1.0.0.dylib”, “Reason: image not found” with flask_mysqldb

空扰寡人 提交于 2020-01-03 04:42:04
问题 I'm trying out Python 3 with Flask and I'm stuck with the following error while working with databases. I'm doing this on macOS High Sierra v. 10.13.6 My import code is as follows: from flask import Flask, render_template, flash, redirect, url_for, session, request, logging from data import Articles from flask_mysqldb import MySQL from wtforms import Form, StringField, TextAreaField, PasswordFeild, validators from passlib.hash import sha256_crypt The error I get when trying to run the app, is

MySQLdb problems in django

元气小坏坏 提交于 2020-01-03 02:20:07
问题 I installed mysql-python on 64bit snow leopard,and it's good under python IDE,but failed import in django. Anyone had meet similar question? File "/Library/Python/2.6/site-packages/django/db/backends/mysql/base.py", line 14, in <module> raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/szanlin/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not

SELECT UNCOMPRESS(text) FROM with sqlalchemy

杀马特。学长 韩版系。学妹 提交于 2020-01-02 23:23:04
问题 I store the MySQL Compress function to insert compressed blob data to the database. In a previous question I was instructed to to use func.compress ( mysql Compress() with sqlalchemy ) The problem now is that I want to read also the data from the database. In mysql I would have done SELECT UNCOMPRESS(text) FROM ... probably I should use a getter in the class. I tried to do somethin like: get_html(self): return func.uncompress(self.text) but this does not work. It returns an sqlalchemy.sql

SELECT UNCOMPRESS(text) FROM with sqlalchemy

最后都变了- 提交于 2020-01-02 23:21:32
问题 I store the MySQL Compress function to insert compressed blob data to the database. In a previous question I was instructed to to use func.compress ( mysql Compress() with sqlalchemy ) The problem now is that I want to read also the data from the database. In mysql I would have done SELECT UNCOMPRESS(text) FROM ... probably I should use a getter in the class. I tried to do somethin like: get_html(self): return func.uncompress(self.text) but this does not work. It returns an sqlalchemy.sql

Django + MySQL - Admin Site - Add User - OperationalError - SAVEPOINT does not exist

和自甴很熟 提交于 2020-01-02 18:39:20
问题 We're trying to have a custom User model and behaviors, but then we noticed that even the default Django installation has issue when adding a new User via the Django Admin: The issue happens even in other Django versions (tried it in Django 1.8 , and w/ the latest one, Django 1.11.3 ). Surprisingly, the issue does not happen when using SQLite or PostgreSQL databases. Also, adding user via $./manage.py createuser and programmatically will work. Editing existing uses like the previously created

ImportError: No module named mysql.base, in django project on Ubuntu 11.04 server

扶醉桌前 提交于 2020-01-02 03:56:09
问题 I am following the steps in the Django Book and got to the part where the authors explain hot wo set up a django project to use a database. I chose mysql. My settings in settings.py are: DATABASES = { 'default': { 'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'mydatabase', # Or path to database file if using sqlite3. 'USER': 'myname', # Not used with sqlite3. 'PASSWORD': 'mypassword', # Not used with sqlite3. 'HOST': '', # Set to empty string for

MySQLdb with multiple transaction per connection

本秂侑毒 提交于 2020-01-02 03:24:05
问题 Is it okay to use a single MySQLdb connection for multiple transactions without closing the connection between them? In other words, something like this: conn = MySQLdb.connect(host="1.2.3.4", port=1234, user="root", passwd="x", db="test") for i in range(10): try: cur = conn.cursor() query = "DELETE FROM SomeTable WHERE ID = %d" % i cur.execute(query) cur.close() conn.commit() except Exception: conn.rollback() conn.close() It seems to work okay, but I just wanted to double check. 回答1: I think

How to test database connectivity in python?

孤者浪人 提交于 2020-01-02 00:54:10
问题 I am accessing a MySQL database from python via MySQLdb library. I am attempting to test the database connection as shown below. db = MySQLdb.connect(self.server, self.user, self.passwd, self.schema) cursor = db.cursor() try: cursor.execute("SELECT VERSION()") results = cursor.fetchone() ver = results[0] if (ver is None): return False else: return True except: print "ERROR IN CONNECTION" return False Is this the right way one should test the connectivity when writing unit testcases? If there