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