Python使用ADO连接数据库的问题

Mask142857 2018-10-25 03:38:12
#-*- coding: utf-8 -*-
import win32com.client
from adoconstants import *
import time


DSN = r"Provider=KRTDBProvider.King.35;Persist Security Info=True;User ID=sa;Password=sa;Data Source=192.168.100.101"
Command = win32com.client.Dispatch(r"ADODB.Command")
Command.ActiveConnection = DSN

Command.CommandText = "SET @@SamplingMode = 'Stepped';SET @@SamplingInterval = 1000;SELECT TagName, DataTime, DataValue FROM History WHERE datatime BETWEEN '2018-10-1 00:00:01' AND '2018-10-1 3:00:00' AND TagName = 'name'"
#Command.CommandText = 'SELECT TagName, DataTime, DataValue FROM History WHERE datatime BETWEEN \'2018-10-1 00:00:01\' AND \'2018-10-1 3:00:00\' AND TagName = \'name\''
Command.CommandType = 8
Command.CommandTimeout = 0
Command.Prepared = True
(rs1,result) = Command.Execute()
while not rs1.EOF :
print(rs1.Fields.Item(1).Value)
rs1.MoveNext()

一段连接工业库取数据的代码
上面的代码会报错pywintypes.com_error: (-2147352567, '发生意外。', (0, 'ADODB.Recordset', '对象关闭时,不允许操作。', 'C:\\Windows\\HELP\\ADO270.CHM', 1240653, -2146824584), None)
不懂什么原因,如果用下面注释掉的SQL只用Select就没有问题,Set系统变量就会报错,有没有大神帮忙,个人感觉是ADO的参数设置问题,求教!
...全文
328 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mask142857 2018-12-07
  • 打赏
  • 举报
回复
最后自己研究出来了,每个分号是按照一个sql语句执行的,但是前两个set语句是返回空的recordset的,因此用NextRecordset()这个函数获取下一个结果就可以: (rs2,result) = rs1.NextRecordset()
Mask142857 2018-10-31
  • 打赏
  • 举报
回复
引用 3 楼 seakingx 的回复:
Command.CommandType = 8 有可能是这个的设置。 一般都是1 ,对应AdCmdText, 你的多语句不知道用啥值。
Command.CommandType = 8是未知类型啊,和1的文本类型没有冲突,所有的参数都试过了。 这个库是只能查询的,但是估计select没问题,其他也没问题。
陈年椰子 2018-10-26
  • 打赏
  • 举报
回复
先试试 执行两条语句是否正常, 比如第一条insert , 第二条 update
陈年椰子 2018-10-26
  • 打赏
  • 举报
回复
Command.CommandType = 8

有可能是这个的设置。 一般都是1 ,对应AdCmdText, 你的多语句不知道用啥值。

Mask142857 2018-10-26
  • 打赏
  • 举报
回复
引用 1 楼 seakingx 的回复:
会不是ADO不支持Set 这种方式。 分成两句行不行?set 执行一次, select 执行一次。
试过了,分开执行不行的,因为这个set是设置的返回的数据格式,分开的话,这句话就无效了,相当于只执行了一个selece。 ADO应该是支持的,用c++的代码的话就没有问题,但是也完全看不出这么写和c++有什么区别啊,就是不好用……

void COLEDBSampleDlg::OnBnClickedSearchdatabn()
{

	HRESULT hr;
	UpdateData(true);
	CString strError;  
	if(m_SQLCommand==_T(""))
	{
		strError.Format (_T("SQL command string is empty!Please type ")); 
		AfxMessageBox(strError);
		return;
	}
	_variant_t vtEmpty;
	VARIANT RecordsAffected;
	
	try
	{
		pRecordset = pConnection->Execute((LPCTSTR)m_SQLCommand,
			&vtEmpty, adCmdUnknown);

		if(pRecordset==NULL)
		{
			strError.Format (_T("SQL Search Failed, check command "));
			AfxMessageBox(strError);
			return;
		}

		while( pRecordset!=NULL )
		{
			long countColumn=0;
			hr=pRecordset->Fields->get_Count(&countColumn);
			if( countColumn > 0 )
				ShowData(pRecordset);

			VariantInit( &RecordsAffected );
			pRecordset = pRecordset->NextRecordset(&RecordsAffected);
			VariantClear( &RecordsAffected );
		}

	   if (pRecordset != NULL )
	   {
		  if (pRecordset->State == adStateOpen)
			 pRecordset->Close();
	   }


	}
陈年椰子 2018-10-26
  • 打赏
  • 举报
回复
会不是ADO不支持Set 这种方式。
分成两句行不行?set 执行一次, select 执行一次。

37,716

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧