最近一段时间在Qt中使用Oracle数据库积累了一些踩坑过程,以及qt使用Oracle数据库的一些操作。
TARGET = qsqloci
HEADERS += $$PWD/qsql_oci_p.h
SOURCES += $$PWD/qsql_oci.cpp $$PWD/main.cpp
#根据Oracle客户端安装路径 指定oci.dll
QMAKE_LFLAGS +=D:/WINDOWS.X64_193000_db_home/oci/lib/msvc/oci.lib
#根据Oracle客户端D安装路径 指定头文件目录
INCLUDEPATH += D:/WINDOWS.X64_193000_db_home/oci/include
#根据安装的Oracle客户端指定libPath
LIBPATH += D:/WINDOWS.X64_193000_db_home/oci/lib/MSVC
darwin:QMAKE_LFLAGS += -Wl,-flat_namespace,-U,_environ
OTHER_FILES += oci.json
PLUGIN_CLASS_NAME = QOCIDriverPlugin
include(../qsqldriverbase.pri)
QSqlDatabase db = QSqlDatabase::addDatabase("QOCI", connectName);
db.setHostName("localhost");
db.setDatabaseName("ORCL");//安装时全局数据库名
db.setUserName("system");
db.setPassword("xiaoxin521");
db.setPort(1521);
1.QSqlDatabase的tables可以返回所有表,然后使用contains可以判断是否存在,这里强烈不建议使用此方式,很耗时,Oracle自带表很多。
2.通过一下sql语句可查询Oracle表,**注意:如果Oracle当前使用的库名中表非常多,此方式也会比较耗时,亲测!,可考虑第三种方式**
bool ret = false;
QString qSqlString = QString("select count(*) from user_tables where upper(table_name) = upper('%1')").arg(talbename);
QSqlQuery query(qSqlString, database);
ret = query.exec();
if (ret) {
while (query.next()) {
ret = query.value(0).toBool();
}
}
return ret;
3.第三种方式是采用表中表名,创建一个存储表名的表,用来判断该表是否存在,此方式时间优化很高,代码量需增加。
QString qSqlString = "create table "+talbename+" ("
"id int PRIMARY KEY,"
"time timestamp)";
QSqlQuery query(qSqlString, database);
bool ret = query.exec();
if(!ret)
{
//
}
else {
QString sequence = "create sequence " + talbename +"_seq "
"increment by 1 "
"start with 1 "
"maxvalue 1000000 "
"minvalue 1 "
"nocache "
"nocycle";
QSqlQuery querySequence(sequence, database);
ret = querySequence.exec();
if(!ret)
{
//
}
}
return ret;
QString qSqlString="insert into "+talbename+ QString(" values(%1,?)").arg(talbename + "_seq.nextval");
QDateTime stopTime= tempData.time.addDays(1);
QString qSqlString = QString("select * from %1 WHERE time between to_date('%2','yyyy/MM/dd') and to_date('%3','yyyy/MM/dd')")
.arg(tableName)
.arg(tempData.time.toString("yyyy-MM-dd"))
.arg(stopTime.toString("yyyy-MM-dd"));
QString qSqlString = "create table test (id float)";
QVariantList varList;
for (double i = 0.1001 ; i < 100000000.0; i+=100.1001) {
varList<< 1.12;
}
String qSqlString= "insert into test(id) values(:id)";
QSqlQuery query(getSqlConnect("xiaoxin"));
query.prepare(qSqlString);
query.bindValue(":id", varList);
qDebug() << query.execBatch() << varList.size();
QString qSqlString = "SELECT * FROM (SELECT * FROM test ORDER BY id DESC) WHERE ROWNUM = 1";
QSqlQuery query(getSqlConnect("xiaoxin"));
query.prepare(qSqlString);
qDebug() << query.exec() ;
qDebug() << query.next() ;
qDebug() << query.value("id").toInt();
1.创建bigfile ORCL为表空间名 datafile为存储的文件路径 size为初始大小
create bigfile tablespace ORCL datafile 'D:\app\oradata\SYSTEM\ORCL01.DBF' size 500M autoextend on;
2.创建一个用户ORCL,结尾ORCL是表空间名
create user ORCL identified by Admin123 default tablespace ORCL;
3.给用户授权
grant connect , resource,dba to ORCL;
4.查询该用户的表空间使用名称
SELECT default_tablespace from dba_users where username = 'ORCL';
5.如果不是ORCL改一下,然后commit提交一下。
alter user ORCL default tablespace ORCL;
操作以上步骤后,需要使用该用户即可。
完结!