网站建设人员招聘,常德市住房和城乡建设局网站,哈尔滨建设工程招投标信息网,重庆百度关键词推广这个居然也冒出来#xff0c;刨坟了。我们不喜欢写原生SQL语句#xff0c;那个写着费劲#xff0c;日常开发时候#xff0c;我们怎么CRUD数据库呢#xff1f;一般使用ORM#xff0c;对象关系映射(英语#xff1a;Object Relational Mapping#xff0c;简称ORM)。主力使…这个居然也冒出来刨坟了。我们不喜欢写原生SQL语句那个写着费劲日常开发时候我们怎么CRUD数据库呢一般使用ORM对象关系映射(英语Object Relational Mapping简称ORM)。主力使用的是python语言那么python中最好用的ORM就是sqlalchemy。一连接参数sqlalchemy使用 create_engine() 函数从URL生成一个数据库链接对象URL遵循 RFC-1738标准。我也不懂。大概就是要有用户名密码地址端口数据库名还有一些可选参数。一个标准的链接URL是这样的dialectdriver://username:passwordhost:port/databasedialect是数据库类型大概包括sqlite, mysql, postgresql, oracle, or mssql.driver是使用的数据库API驱动连接包随便叫什么吧。username用户名password密码host网络地址可以用ip域名计算机名当然是你能访问到的。port数据库端口。databas数据库名。其实这些也就dialect和dirver需要解释。二连接sqlite31驱动sqlite3是个文件数据库不需要什么驱动或者说python内置了驱动。2标准连接参数# sqlite:///没有hostname3各种链接参数# 相对路径就是这个python文件同目录下foo.dbengine create_engine(sqlite:///foo.db)#绝对路径#Unix/Mac下用四条表示engine create_engine(sqlite:absolute/path/to/foo.db)#Windows下用三条///加盘符路径用两条\\engine create_engine(sqlite:///C:\\path\\to\\foo.db)#Windows 也可以这么用三条///加盘符路径用一条\engine create_engine(rsqlite:///C:\path\to\foo.db)#数据库建在内存里。URI保持为空即可engine create_engine(sqlite://)三连接mysql(mariadb)sqlalchemy默认使用mysql-python作为链接驱动既default模式选哪种驱动就装哪个包。1default默认链接方式engine create_engine(mysql://scott:tigerlocalhost/foo)2# mysql-python声明使用mysql-python驱动engine create_engine(mysqlmysqldb://scott:tigerlocalhost/foo)3MySQL-connector-python 声明使用MySQL-connector-python驱动(推荐使用)engine create_engine(mysqlmysqlconnector://scott:tigerlocalhost/foo)4OurSQL 声明使用OurSQL驱动engine create_engine(mysqloursql://scott:tigerlocalhost/foo)四连接Microsoft SQL Serversqlalchemy默认使用 pyodbc作为链接驱动。1pyodbcengine create_engine(mssqlpyodbc://scott:tigermydsn)2pymssqlengine create_engine(mssqlpymssql://scott:tigerhostname:port/dbname)五连接PostgreSQLPostgreSQL默认使用 psycopg2作为链接驱动既default模式1 defaultengine create_engine(postgresql://scott:tigerlocalhost/mydatabase)2psycopg2engine create_engine(postgresqlpsycopg2://scott:tigerlocalhost/mydatabase)3 pg8000engine create_engine(postgresqlpg8000://scott:tigerlocalhost/mydatabase)六连接OracleOracle可能只有 cx_oracle一个驱动包既default模式和声明模式一样。1defaultengine create_engine(oracle://scott:tiger127.0.0.1:1521/sidname)2cx_oracleengine create_engine(oraclecx_oracle://scott:tigertnsname)