Python模拟Oracle的SQL/PLUS工具的实现办法[Oracle防范]
本文“Python模拟Oracle的SQL/PLUS工具的实现办法[Oracle防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
loginstr=raw_input("login>").strip();
if loginstr=="" :
continue;
elif loginstr in ["exit","exit;"]:
print("…bye…");
exit();
conn = getConnect(loginstr);
if connectresult==1:
break;
except KeyboardInterrupt:
print("^C");
continue;
while 1:
sqlstr="";
try:
sqlstrline=raw_input(promptstr+">").strip();
if sqlstrline=="" :
continue;
elif sqlstrline.lower() in ["exit","exit;"]:
print("…bye…");
exit();
elif sqlstrline[0:7].lower()=="connect" :
conn = getConnect(sqlstrline[8:]);
elif sqlstrline.lower() in ["disconnect","disconnect;"] :
conn.close();
print("Connection closed.");
elif sqlstrline[0:4].lower()=="host" :
os.system(sqlstrline[4:])
else:
sqlstrsqlstr=sqlstr+sqlstrline+'\n';
while sqlstrline[-1]!=";" :
sqlstrline=raw_input().strip();
sqlstrsqlstr=sqlstr+sqlstrline+'\n';
sqlstrsqlstr=sqlstr[0:len(sqlstr)-2]
try:
cursor = conn.cursor();
cursor.execute(sqlstr);
if sqlstr[0:6].lower()=="select" :
cols=[]
for col in cursor.description:
print(getcolformatstr(col) % (col[0])),
print('');
for col in cursor.description:
if col[1]==cx_Oracle.NUMBER:
print('-'*12),;
else:
if col[2]<=32:
print('-'*col[2]),;
else:
print('-'*32),;
print('');
recs = cursor.fetchmany(fetchsize);
while len(recs)>0:
for row in recs:
for i in range(len(row)):
if row[i]!=None:
print(getcolformatstr(cursor.description[i]) % row[i]),;
else:
print(getcolformatstr(cursor.description[i]) % ''),;
print('')
recs = cursor.fetchmany(fetchsize);
print(str(cursor.rowcount)+" rows selected.");
elif sqlstr[0:6].lower()=="insert" :
print(str(cursor.rowcount)+" rows inserted.");
elif sqlstr[0:6].lower()=="update" :
print(str(cursor.rowcount)+" rows updated.");
elif sqlstr[0:6].lower()=="delete" :
print(str(cursor.rowcount)+" rows deleted.");
elif sqlstr[0:5].lower()=="merge" :
print(str(cursor.rowcount)+" rows merged.");
elif sqlstr[0:6].lower()=="commit" :
print("Commit complete.");
elif sqlstr[0:6].lower()=="rollback" :
print("Rollback complete.");
else :
print("sql execute complete.");
except cx_Oracle.InterfaceError as exc:
error, = exc.args
print(exc);
except cx_Oracle.DatabaseError as exc:
error, = exc.args
print(error.message);
except KeyboardInterrupt:
print("^C");
continue;
分外阐明:纯属个人学习python的代码,未考虑扩大性与性能,未经过专业测试,不倡议拿到实际工作中当SQLPLUS用.
关于Python模拟Oracle的SQL/PLUS工具的实现办法就介绍到这里了,但愿本次的介绍可以对您有所收获!
以上是“Python模拟Oracle的SQL/PLUS工具的实现办法[Oracle防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |