Python 数据库支持

DB-API 模块属性

import sqlite3
sqlite3.apilevel #模块兼容的DB-API版本号
sqlite3.threadsafety #线程安全级别 : 0 不支持线程安全,多个线程不能共享模块 1初级线程安全, 线程可以共享模块,不能共享连接 2中级,可以共享模块和连接,不能共享游标 3完成线程安全,可以共享模块\连接\游标
sqlite3.paramstyle #支持SQL语句参数风格 数字风格 WHERE name=:1 || 命名风格 WHERE name=:name || Python风格 WHERE name=%(name)s || 问号风格 WHERE name=? || 标准ANSI C 格式转换 WHERE name=%s
sqlite3.connect() #连接函数 

数据库连接

方式一:
import 数据库模块
myConnect = 数据库模块.connect()
myConnect.query("SELECT personName FROM tableNmae ")
myConnect.commit()
myConnect.close()

方式二:
import 数据库模块
myConnect = 数据库模块.connect()
游标 = myConnect.cuisor()
游标.excute(“SQL语句”)
results = 游标.fetchall()
游标.close()
myConnect.commit()
myConnect.close()

connect()

user : 账户
password: 密码
host: host name
database: database name
dsn: data source name

MySQLdb.connect(host="dbserver", db="inv", user="James")
PgSQl.connect(database="sales")
psycopg.connect(database="template", user="pgsql")
gadfly.dbapi20.connect("csrDB", "/usr/local/database")
sqlite3.connect("marketing/test")
发布了48 篇原创文章 · 获赞 0 · 访问量 568

猜你喜欢

转载自blog.csdn.net/weixin_44286839/article/details/104203246
今日推荐