python連接mysql數據庫遇到的問題


1.源代碼:

 

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column
from sqlalchemy.types import CHAR, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from random import randint
from sqlalchemy import ForeignKey
engine = create_engine('mssql+pymssql://root:root@localhost:3307/python',encoding="utf-8", echo=True)#創造一個連接
Base=declarative_base()#生成ORM基類
#定義一個類Host,一個表對應一個類,且這個類和表做了映射關系
class Host(Base):
    tablename="hostinfo"#表名
    id=Column(Integer,primary_key=True)#字段
    hostname=Column(String(32))#字段
    ip=Column(String(64))#字段
Base.metadata.create_all(engine)#創建表結構
Session_class=sessionmaker(bind=engine)#創建與數據庫的會話session class
Session=Session_class()#生成session實例
user_obj=Host(hostname="pc1",ip="192.168.1.3")#生成你要創建的數據對象
print(user_obj.hostname,user_obj.ip,user_obj.id)#打印數據
Session.add(user_obj)#把要創建的數據對象添加到這個session里,一會統一創建

Session.commit()

報錯:

Traceback (most recent call last):
  File "F:/Python_Document/sql/3.py", line 11, in <module>
    class Host(Base):
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\ext\declarative\api.py", line 65, in __init__
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\ext\declarative\base.py", line 88, in _as_declarative
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\ext\declarative\base.py", line 116, in setup_mapping
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\ext\declarative\base.py", line 146, in __init__
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\ext\declarative\base.py", line 472, in _setup_inheritance
sqlalchemy.exc.InvalidRequestError: Class <class '__main__.Host'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.

 解決辦法:源代碼中的_tablename_格式寫錯,把報錯信息中的__tablename__    復制過去就對了。

2.

源代碼:

 

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column
from sqlalchemy.types import CHAR, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from random import randint
from sqlalchemy import ForeignKey
engine=create_engine('mssql+pymssql://root:root@localhost:3307/student')#創造一個連接
Base=declarative_base()#生成ORM基類
#定義一個類Host,一個表對應一個類,且這個類和表做了映射關系
class Host(Base):
    __tablename__='hostinfo'#表名
    id=Column(Integer,primary_key=True)#字段
    hostname=Column(String(32))#字段
    ip=Column(String(64))#字段
Base.metadata.create_all(engine)#創建表結構
Session_class=sessionmaker(bind=engine)#創建與數據庫的會話session class
Session=Session_class()#生成session實例
user_obj=Host(hostname="pc1",ip="192.168.1.3")#生成你要創建的數據對象
print(user_obj.hostname,user_obj.ip,user_obj.id)#打印數據
Session.add(user_obj)#把要創建的數據對象添加到這個session里,一會統一創建
Session.commit()

報錯:

Traceback (most recent call last):
  File "F:/Python_Document/sql/2.py", line 19, in <module>
    Base.metadata.create_all(engine)#創建表結構
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\sql\schema.py", line 4005, in create_all
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\engine\base.py", line 1939, in _run_visitor
  File "D:\python3.6\lib\contextlib.py", line 82, in __enter__
    return next(self.gen)
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\engine\base.py", line 1932, in _optional_conn_ctx_manager
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\engine\base.py", line 2123, in contextual_connect
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\engine\base.py", line 2162, in _wrap_pool_connect
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception_noconnection
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\util\compat.py", line 265, in raise_from_cause
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\util\compat.py", line 248, in reraise
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\engine\base.py", line 2158, in _wrap_pool_connect
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\pool.py", line 403, in connect
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\pool.py", line 791, in _checkout
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\pool.py", line 532, in checkout
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\pool.py", line 1196, in _do_get
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\util\langhelpers.py", line 66, in __exit__
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\util\compat.py", line 249, in reraise
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\pool.py", line 1193, in _do_get
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\pool.py", line 350, in _create_connection
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\pool.py", line 477, in __init__
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\pool.py", line 674, in __connect
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\engine\strategies.py", line 106, in connect
  File "D:\python3.6\lib\site-packages\sqlalchemy-1.2.10-py3.6-win-amd64.egg\sqlalchemy\engine\default.py", line 412, in connect
  File "pymssql.pyx", line 641, in pymssql.connect (pymssql.c:10824)
sqlalchemy.exc.OperationalError: (pymssql.OperationalError) (20002, b'DB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (localhost:3307)\n') (Background on this error at: http://sqlalche.me/e/e3q8)
Process finished with exit code 1
解決辦法:
后來發現是在mysql中創建不了表格。所以只能手動創建表格。但是任然與數據庫不能連接。
然后我配置了sql server數據庫。發現連接成功。
源代碼:
import pymssql
#連接sql server數據庫

conn=pymssql.connect(host="localhost",port=1433,user="sa", password="root",database="world",charset="utf8")
cursor = conn.cursor()
sql = "select * from Product"
cursor.execute(sql)

# 獲取總記錄數
print(cursor.rowcount)

# 獲取一條數據
rs = cursor.fetchone()
print(rs)

# 獲取所有數據,返回所有的數據
rs = cursor.fetchall()
print(rs)

cursor.close()
conn.close()

連接不上mysql數據庫,應該是數據庫的問題。應該再重裝一遍數據庫就行了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM