Trying to use Flask-mysql with python - 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()

Related

How to use psycopg2 module using Mac

I am trying to import psycopg2 in a python file I created to establish a connection with my Postgres database. Issue is that my system shows I have psycopg2 installed when I run the following in my terminal pip freeze | grep psycopg2 but in my .py file I have the following code
import psycopg2
conn = psycopg2.connect('dbname=test user=postgres')
cur = conn.cursor()
cur.execute("SELECT * FROM my_data")
records = cur.fetchall()
and it fails at the import. The problem I get is Import "psycopg2" count not be resolved from source Pylance(reportMissingModuleSource)
What am I doing wrong. I have psycopg2-binary installed as well. My Mac version is Big Sur version 11.6
My psycopg2 version is 2.8.6

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?

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:bigquery

Trying to create a bigquery connector using sqlalchemy
from sqlalchemy import create_engine
engine = create_engine('bigquery://<project_id>/<project_name>',
credentials_path=GCP_KEY)
conn = engine.connect()
Error:
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:bigquery
Your error is generally related to the lack of some required module to use SQLAlchemy.
Therefore, after going though the documentation, I found out that you should install the requirements in you environment using:
pip3 install pybigquery
In addition, within your script you should import the following modules:
SQLAchemy
from sqlalchemy import *
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import *
API Client
from pybigquery.api import ApiClient
Afterwards, you should have all the necessary packages to execute your code.
If you have any more question about using the SQLAlchemy and API client for BigQuery, you can consult the provided documentation above or I would also be glad to help.

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)
# ...

Python 3.2 SQLite 3 Error with connect function

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

Categories

Resources