MySQLdb can't use cursorclass - python

I am trying to execute the following code:
import MySQLdb
import MySQLdb.cursors
conn=MySQLdb.connect(host = '127.0.0.1',
user = 'root',
passwd = 'root',
db = 'test',
cursorclass = MySQLdb.cursors.DictCursor)
cursor=conn.cursor()
But it gives me the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 243, in cursor
AttributeError: 'Connection' object has no attribute 'cursorclass'
Why is this?

import MySQLdb
import MySQLdb.cursors
conn=MySQLdb.connect(host = '127.0.0.1',
user = 'root',
passwd = 'root',
db = 'test',)
cursor=conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
This is a rough hack, but it may not be advisable to use it. The script with throw out dictionary object by default but the application might require an tuple or array.

Related

Getting AttributeError: module 'mysql' has no attribute 'connector' in python

I am on windows 10 and using python3.9.
I installed the pagkage
>python3 -m pip install mysql-connector-python
Now I try to run a simple program
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="pythonDB"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FORM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
I am getting the following error when running
>python3 select.py
Traceback (most recent call last):
File "G:\Python_w3school\mysql\select.py", line 1, in
import mysql.connector
File "C:\Users\pawar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector_init_.py", line 42, in
import dns.resolver
File "C:\Users\pawar\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\dns\resolver.py", line 20, in
import socket
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\socket.py", line 54, in
import os, sys, io, selectors
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\selectors.py", line 12, in
import select
File "G:\Python_w3school\mysql\select.py", line 3, in
mydb = mysql.connector.connect(
AttributeError: module 'mysql' has no attribute 'connector'
try these:
AttributeError: module 'mysql' has no attribute 'connector'
Attribute error:module 'mysql' has no attribute 'connector'
basically change the import to
from mysql import connector
Seems like the Python3 is not taking SQL directly inside the execute function.
Assign the query to a variable and pass it to the execute() function.
It worked for me!!!
sql = "SELECT * FROM customers"
mycursor.execute(sql)
Also try try renaming the file if it is something like select.py to select_customers.py

Why does mysql.connector.connect fail with internal error?

I'm upgrading a database application from Python 2.7 to 3.4. I originally used mysqldb, but I'm trying to switch to mysql-connector. My database connection fails with an internal TypeError. Here's the code and the error:
import mysql.connector
try:
dbh = mysql.connector.connect("localhost","pyuser","pypwd","jukebox")
except mysql.connector.Error as err:
print("Failed opening database: {}".format(err))
exit(1)
and here's the error:
# python loadcd.py
Traceback (most recent call last):
File "loadcd.py", line 12, in <module>
dbh = mysql.connector.connect("127.0.0.1","pyuser","pypwd","jukebox")
File "/usr/local/lib/python3.4/dist-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/mysql/connector/connection.py", line 57, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 5 were given
I'm at a loss. The exact same connection works with mysqldb, and I can connect with the same credentials using PHP or at the command line.
You should provide keyword arguments:
dbh = mysql.connector.connect(host="localhost",user="pyuser",password="pypwd",database="jukebox")
Use this and replace the values as per your configuration:
import mysql.connector
mydb = mysql.connector.connect(host="localhost",
user="yourusername",
password="yourpassword")
print(mydb)

Error while working with sqlite3 in python

In my Python script, I am seeing the following error.
Traceback (most recent call last):
File "F:/python sqllite/test32.py", line 3,
in <module> conn.sqlite3.connect('test.db')
AttributeError: 'sqlite3.Connection' object has no attribute 'sqlite3'
Does anyone have an idea why I might be seeing this?
Change: conn.sqlite3.connect('test.db')
to : conn = sqlite3.connect('test.db')

Error while open app after using pyinstaller

I'm getting error while open my app after using pyinstaller before using pyinstaller when the app still in .py it's working 100%
after that I'm getting this error
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "c:\users\deounix\appdata\local\temp
.5-py2.7-win32.egg.tmp\MySQLdb\__init__.py"
File "c:\users\deounix\appdata\local\temp
.5-py2.7-win32.egg.tmp\_mysql.py", line 7,
File "c:\users\deounix\appdata\local\temp
.5-py2.7-win32.egg.tmp\_mysql.py", line 3,
ImportError: No module named pkg_resources
what I'm doing is :
#!/usr/bin/python
import MySQLdb
from time import sleep
db = MySQLdb.connect(host="localhost", user="root", port=3306, passwd="123456", db="test")
cur = db.cursor()
cur.execute("SHOW TABLES")
for row in cur.fetchall() :
print row
sleep(10)

Create dynamic model in Django 1.8

How to create Dynamic models in Django 1.8?
I have done the following according to the official site:
>>attrs = {
'name': models.CharField(max_length=32),
'__module__': 'myapp.models'
}
>>person = type("ptable", (models.Model,), attrs)
>>def install(model):
from django.core.management import sql, color
from django.db import connection
style = color.no_style()
cursor = connection.cursor()
statements, pending = sql.sql_model_create(model, style)
for sql in statements:
cursor.execute(sql)
>>install(person)
But it gives me this error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 6, in install
AttributeError: 'module' object has no attribute 'sql_model_create'
How to solve it?

Categories

Resources