跳到主要内容

创建数据库对象

命令说明

创建数据库对象

命令原型

objDatabase = Database.CreateDB(dbtype,dbDict)

命令参数

参数必选类型默认值说明
dbtypeTrueenum"MySQL"数据库的类型
dbDictTruemultiDictionary{"host": "", "port": "3306", "user": "","password": "","database": "","charset": "utf8"}连接数据库的配置字典

返回结果

objDatabase,将命令运行后的结果赋值给此变量。

运行实例

/*********************************创建数据库对象***************************************
命令原型:
Database.CloseDB(objDatabase)
入参:
dbtype--数据库的类型
dbDict--连接数据库的配置字典
objDatabase--命令运行后的结果
注意事项:
数据库连接使用完后记得及时关闭
连接命令的charset参数需要与服务端保持一致
Oracle数据库前置条件:
1.安装oracle客户端(这里假设安装到C:\instantclient_11_2,下面配置需要替换为实际路径)
2.在“环境变量”的“系统变量”中增加:
ORACLE_HOME = C:\instantclient_11_2
TNS_ADMIN = C:\instantclient_11_2
NLS_LANG = SIMPLIFIED CHINESE_CHINA.ZHS16GBK
3.修改Path变量,在后面添加 C:\instantclient_11_2
4.在C:\instantclient_11_2 新建一个tnsnames.ora文件,增加自己的数据库别名配置。
示例如下:
MyDB=(DESCRIPTION=(ADDRESS= (PROTOCOL = TCP)(HOST= 172.16.1.16)(PORT = 1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SERVICE_NAME=ora10g)
)
)
修改HOST、PORT、SERVICE_NAME与Oracle服务端对应
**********************************************************************************/
Dim objDatabase
Dim ip,port,username,password,db

//*********************************MySQL***************************************
// 连接MySQL数据库
ip = "127.0.0.1"
port = "3306"
username = "root"
password = "rg+d2Wr8T+Dv10iQBk7VUw=="
db = "test"
objDatabase = Database.CreateDB("MySQL", {
"host": ip,
"port": port,
"user": username,
"password": password,
"database": db,
"charset": "utf8"
})

//*********************************PostgreSQL***************************************
// 连接PostGreSQL数据库
ip = "127.0.0.1"
port = "5432"
username = "root"
password = "rg+d2Wr8T+Dv10iQBk7VUw=="
db = "postgres"
objDatabase = Database.CreateDB("PostgreSQL", {
"host": ip,
"port": port,
"user": username,
"password": password,
"database": db
})

//*********************************Sqlite3***************************************
// 连接Sqlite3数据库
objDatabase = Database.CreateDB("Sqlite3", {"filepath": '''D:\工作文档\sqlite\test.db'''})

//*********************************SQLServer***************************************
// 连接SQLServer数据库
ip = "127.0.0.1"
port = "1433"
username = "SA"
password = "rg+d2Wr8T+Dv10iQBk7VUw=="
db = "TestDB"
objDatabase = Database.CreateDB("PostgreSQL", {
"host": ip,
"port": port,
"user": username,
"password": password,
"database": db,
"charset": "utf8"
})

//*********************************Oracle***************************************
// 连接Oracle数据库
ip = "127.0.0.1"
port = "1521"
username = "oracle"
password = "OvUZIny9qZUrgJE0ho2tnQ=="
service_name = ""
sid = "helowin"
objDatabase = Database.CreateDB("Oracle", {
"host": ip,
"port": port,
"user": username,
"password": password,
"service_name": service_name,
"sid": sid,
"charset": "utf8"
})

可视化样例