标签:into 当前目录 cal 执行 localhost creat nal type nmake
No1:
【SQLite】
插入
# 导入SQLite驱动: >>> import sqlite3 # 连接到SQLite数据库 # 数据库文件是test.db # 如果文件不存在,会自动在当前目录创建: >>> conn = sqlite3.connect(‘test.db‘) # 创建一个Cursor: >>> cursor = conn.cursor() # 执行一条SQL语句,创建user表: >>> cursor.execute(‘create table user (id varchar(20) primary key, name varchar(20))‘) <sqlite3.Cursor object at 0x10f8aa260> # 继续执行一条SQL语句,插入一条记录: >>> cursor.execute(‘insert into user (id, name) values (\‘1\‘, \‘Michael\‘)‘) <sqlite3.Cursor object at 0x10f8aa260> # 通过rowcount获得插入的行数: >>> cursor.rowcount 1 # 关闭Cursor: >>> cursor.close() # 提交事务: >>> conn.commit() # 关闭Connection: >>> conn.close()
查询
>>> conn = sqlite3.connect(‘test.db‘) >>> cursor = conn.cursor() # 执行查询语句: >>> cursor.execute(‘select * from user where id=?‘, (‘1‘,)) <sqlite3.Cursor object at 0x10f8aa340> # 获得查询结果集: >>> values = cursor.fetchall() >>> values [(‘1‘, ‘Michael‘)] >>> cursor.close() >>> conn.close()
No2:
【MySQL】
安装mysql驱动
$ pip install mysql-connector-python --allow-external mysql-connector-python
如果上面的命令安装失败,可以试试另一个驱动:
$ pip install mysql-connector
# 导入MySQL驱动: >>> import mysql.connector # 注意把password设为你的root口令: >>> conn = mysql.connector.connect(user=‘root‘, password=‘password‘, database=‘test‘) >>> cursor = conn.cursor() # 创建user表: >>> cursor.execute(‘create table user (id varchar(20) primary key, name varchar(20))‘) # 插入一行记录,注意MySQL的占位符是%s: >>> cursor.execute(‘insert into user (id, name) values (%s, %s)‘, [‘1‘, ‘Michael‘]) >>> cursor.rowcount 1 # 提交事务: >>> conn.commit() >>> cursor.close() # 运行查询: >>> cursor = conn.cursor() >>> cursor.execute(‘select * from user where id = %s‘, (‘1‘,)) >>> values = cursor.fetchall() >>> values [(‘1‘, ‘Michael‘)] # 关闭Cursor和Connection: >>> cursor.close() True >>> conn.close()
No3:
【SQLAlchemy】ORM框架
安装
$ pip install sqlalchemy
并初始化DBSession:
# 导入: from sqlalchemy import Column, String, create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base # 创建对象的基类: Base = declarative_base() # 定义User对象: class User(Base): # 表的名字: __tablename__ = ‘user‘ # 表的结构: id = Column(String(20), primary_key=True) name = Column(String(20)) # 初始化数据库连接: engine = create_engine(‘mysql+mysqlconnector://root:password@localhost:3306/test‘) # 创建DBSession类型: DBSession = sessionmaker(bind=engine)
添加对象
# 创建session对象: session = DBSession() # 创建新User对象: new_user = User(id=‘5‘, name=‘Bob‘) # 添加到session: session.add(new_user) # 提交即保存到数据库: session.commit() # 关闭session: session.close()
查询
# 创建Session: session = DBSession() # 创建Query查询,filter是where条件,最后调用one()返回唯一行,如果调用all()则返回所有行: user = session.query(User).filter(User.id==‘5‘).one() # 打印类型和对象的name属性: print(‘type:‘, type(user)) print(‘name:‘, user.name) # 关闭Session: session.close()
标签:into 当前目录 cal 执行 localhost creat nal type nmake
原文地址:https://www.cnblogs.com/anni-qianqian/p/9275748.html