Python 3.2 SQLite 3 Error with connect function - python

I try to learn SQL and I do that with Python 3.2. I know that SQLite3 module is installed with Python, but when I try to do this code:
import sqlite3
conn = sqlite3.connect('example.db')
I got this error: Undefined variable from import: connect
I have checked In usr/lib and the .so file is there..
What can I do to solve that ?
Regards,

I think you're using pydev.
Try it in non-pydev environ.
>>> import sqlite3
>>> conn = sqlite3.connect('example.db')
UPDATE: The actual fix for this problem is given in the following link

Related

I get ImportError: No module named pyodbc when I try to connect to the sybase database

I am trying to connect to the sybase database using sqlalchemy. I am doing my development in Linux environment. I am also using python 2.7
I have the following code in which I am just trying to connect to the database. When I try run the piece of code with just the import part, I do not get the error ImportError: No module named pyodbc.
I only get the error with the import when I uncomment and run the sal.create_engine part of the code. I am not able to understand why it is saying the it is unable to import the pyodbc.
My code as of now:
import sqlalchemy as sal
from sqlalchemy import create_engine
from sqlalchemy.connectors import pyodbc
# in order to connect, we need server name, database name we want to connect to
engine = sal.create_engine('sybase+pyodbc://dialect+driver://username:password#host:port/database', echo = True)
conn = engine.connect()
# printing names of the tables present in the database
#print(engine.table_names())
May someone help me to resolve this issue?

ModuleNotFoundError - PyMySQL for python 3

I am trying to get a simple test program working on my machine that connects to a SQL DB. I pip installed and then uninstalled and then installed with pip3: pymysql. the issue I'm getting:
import PyMySQL
ModuleNotFoundError: No module named 'PyMySQL'.
it can be found when i run the command pip list, but not found when running the program itself. I was perusing over other SO Q&A's but nothing helped.
Thanks
First insall PyMySQL using:
pip install PyMySQL
In python 3 it is pymysql, all in lower case
import pymysql
Module names are case-sensitive, and if you take a look at this example in the GitHub repo, you'll see that the module should be imported as pymysql, which is consistent with the all-lowercase convention for modules spelled out in the PEP 8 style guide.
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
# ...

Trying to use Flask-mysql with python

I'm trying to get Flask to connect to a local MySQL but this keeps failing. I have tried multiple combinations based on other forums
from flask.ext.mysql import MySQL
from flask_mysql import MySQL
from flaskext.mysql import MySQL
etc.
the error I get is the following
ImportError: No module named flask.ext.mysql
Running Windows 10 & Python3.5
I have pip installed flask-MySQL
********** EDIT ***********
With the help of the user #metmirr (Thank you!!!) the fix was to use a virtual environment, couldn't get it to work any other way! The answer and comments have been removed somehow
Use mysql.connector for accessing MySQL from Flask. For more details visit the link.
Import MySQL Connector
import mysql.connector
Connect to local database
def getMysqlConnection():
return mysql.connector.connect(host='localhost',database='test',user='root',password='root')
To access the database
db = getMysqlConnection()
sqlstr = "select * from test_table"
cur = db.cursor()
cur.execute(sqlstr)
output_json = cur.fetchall()

Wamp Server and Python

Hi i have installed WAMP server(Mysql , apache ,php) and also installed "Python" separately.Now i am trying to connect to that database using python code .
This is my python code:
#!C:\Python32\python.exe
import sys
import os
import cgi
import MySQLdb
import cgitb
import SecureDb
cgitb.enable()
print ("Content-type: text/plain\n\n");
conn= MySQLdb.connect(host = SecureDb.host ,user =SecureDb.user ,passwd=SecureDb.password ,db=SecureDb.database)
cursor=conn.cursor()
cursor.execute("select * from register where Name='Subburaj'")
result=cursor.fetchall()
cursor.close()
conn.close()
But this is showing an error:
Traceback (most recent call last):
File "C:\Users\Ponmani\Desktop\test.cgi", line 5, in <module>
import MySQLdb
File "C:\Python32\lib\site-packages\MySQLdb\__init__.py", line 17, in <module>
from release import __version__, version_info, __author__
ImportError: No module named release
If anybody came across this problem please help me to solve this.Thanks in advance.
MYSQLdb doesn't come with python. It seems that you have to install it first from here. There's an executable here, if you are using windows 32. But it's for python 2.7. If you are using python 3.2 it gets more difficult. Here's an unofficial package that should work for 3.2.
EDIT:
Release module should be a part of mysqldb, my only guess is theres is still something wrong with the installation. Maybe subforlders weren't extracted correctly.You should try to reinstall.
EDIT: you can also check if you have release.py in the mysqldb directory. if you dont it is surely installation problem.
You Need to install MySQLdb Module
easy_install MySQL-python

Importing _mysql in MySQLdb

Why is _mysql in the MySQLdb module a C file? When the module tries to import it, I get an import error. What should I do?
It's the adaptor that sits between the Python MySQLdb module and the C libmysqlclient library. One of the most common reasons for it not loading is that the appropriate libmysqlclient library is not in place.
Edit: This might be the answer to your question.
When I try to import _mysql, I get no error:
import _mysql
print(_mysql)
# <module '_mysql' from '/usr/lib/pymodules/python2.6/_mysql.so'>
It is importing the library /usr/lib/pymodules/python2.6/_mysql.so
If this is not what you are getting, it sounds like an installation error.
What OS are you using?
How did you install mysqldb?

Categories

Resources