群友小伙伴需求
aardio学习群 625494397
需要安装库
需要注意
端口 用户名 密码问题
import com
直接看示例
import com
io.open();
conn = com.CreateObject("ADODB.Connection");
conn.ConnectionString = "Provider=OraOLEDB.Oracle;
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.13)(PORT=1521))(CONNECT_DATA =(SERVER=DEDICATED)(SERVICE_NAME=实例名)));
User Id=用户名;Password=密码;";
conn.Open() ;
rs = com.CreateObject("ADODB.RecordSet")
rs.Open("select * from privatepara order by pparaid", conn )
while(!rs.eof){
io.print(
string.trimright( rs("pparaid").value ),
string.trimright( rs("pparaname").value ),
string.trimright( rs("pparavalue").value )
)
rs.movenext()
}
rs.close();
conn.Close();
群友提供封装Mysql Oracle类
class MSDASQL{
ctor(data_source,user_id,password){
this.provider = "MSDASQL";
this.data_source = data_source;
this.user_id = user_id;
this.password = password;
}
}
class OraOLEDB{
ctor(host,port,service_name,user_id,password){
this.provider = "OraOLEDB.Oracle";
this.host = host;
this.port = port;
this.service_name = service_name;
this.user_id = user_id;
this.password = password;
}
}
class Sql{
ctor(command,in_args,out_fields){
this.command = command;
this.in_args = in_args;
this.out_fields = out_fields;
this.command_final = null;
this.result = {};
};
formatSqlParameters = function(sql,...) {
if( type( ... ) == "table" ){
var param = ...;
return ..string.replace(sql,"\@(<:+>|<\w+>[^\s\p]*)",
function(name){
var v = param[name]
var t = type(v);
if( t = "string" ){
return "'" + ..string.replace(v,"'","''") + "'";
}
elseif( t = "boolean" ){
return v ? "1" : "0";
}
elseif( t = "number" ){
return tostring(v);
}
elseif( ( t = "table" ) && ..time.istime(v) ) {
var f = v.format;
v.format = "%Y-%m-%d %H:%M:%S";
var ret = "'" + tostring(v) + "'";
v.format = f;
return ret;
}
return ..string.replace(tostring(v),"\p","");
}
);
}
return ..string.format(sql,...);
}
tostring = function(){
import console
this.command_final = this.formatSqlParameters(this.command,this.in_args);
return this.command_final;
}
pushResult = function(item){
..table.push(this.result,item);
}
}
class Oracle{
ctor(connObject){
if(!connObject){
error("")
}
import com;
if(connObject.provider=="MSDASQL"){
try{
this.conn = com.CreateObject("ADODB.Connection");
this.conn.ConnectionString = ..string.concat("Provider=MSDASQL; Data Source=",connObject.data_source,";","User Id=",connObject.user_id,";Password=",connObject.password,";");
this.conn.open();
}
catch(e){
this.conn = null;
}
}elseif(connObject.provider=="OraOLEDB.Oracle"){
try{
this.conn = com.CreateObject("ADODB.Connection");
this.conn.ConnectionString = ..string.concat("Provider=OraOLEDB.Oracle; Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=",connObject.host,")(PORT=",connObject.port,"))(CONNECT_DATA =(SERVER=DEDICATED)(SERVICE_NAME=",connObject.service_name,")));","User Id=",connObject.username,";Password=",connObject.password,";");
this.conn.open();
}
catch(e){
this.conn = null;
}
}
};
query = function(sqlObj){
if(!this.conn){
error("数据库未连接")
return;
}
import console;
import com;
var rs;
try{
rs = com.CreateObject("ADODB.RecordSet");
sqlLine = sqlObj.tostring();
rs.open(sqlLine,this.conn);
while(!rs.eof){
var item = {}
for(i=1;#sqlObj.out_fields;1){
..table.push(item,rs(sqlObj.out_fields[i]).value);
}
sqlObj.pushResult(item);
rs.movenext()
}
}
catch(e){
error(e);
}
try{
rs.close();
rs = null;
}
return sqlObj;
}
close = function(){
try{
this.conn.close();
this.conn = null;
}
catch(e){
this.conn = null;
error("连接关闭异常")
}
}
}