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

1.源代碼:python

 

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()

報錯:mysql

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.sql

 解決辦法:源代碼中的_tablename_格式寫錯,把報錯信息中的__tablename__    複製過去就對了。數據庫

2.api

源代碼:session

 

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()

報錯:app

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數據庫,應該是數據庫的問題。應該再重裝一遍數據庫就好了。dom

相關文章
相關標籤/搜索