我正在使用Delphi(XE3)并需要连接到MySQL数据库。我有一个看似很常见的奇怪问题,但我还没有完全解决这个问题。
传统解决方案包括:
问题1: 启动应用程序,执行代码:如果新值恰好匹配数据库中已有的值,我会在位置" B"
处得到错误。问题2: 启动应用程序,执行代码:如果新值恰好与数据库中已有的值不同,它将成功执行一次,然后在位置给出错误&#34; A&#34;。< / p>
由于记录主键未被更改,因此在任何时候找到记录都不会有任何问题。
object conMain: TADOConnection
Connected = True
ConnectionString =
'Provider=MSDASQL.1;Password=p;Persist Security Info=True;U' +
'ser ID=M;Extended Properties="Driver={MySQL ODBC 5.3 ANSI Dri' +
'ver};SERVER=yukon;DATABASE=db;UID=M;Pwd=p;PORT=3306;' +
'"'
LoginPrompt = False
Mode = cmShareDenyNone
Left = 48
Top = 24
end
object ADOTable1: TADOTable
Connection = conMain
IndexFieldNames = 'FacilityID'
TableName = 'facility'
Left = 152
Top = 24
end
procedure TForm1.Button1Click(Sender: TObject);
begin
conMain.Open;
ADOTable1.Open;
ADOTable1.Properties.Item['Update Criteria'].Value:=adCriteriaKey;
// **A**
if ADOTable1.Locate('facilityid', '{C0FADCC8-15C9-48C8-8003-3BBD4AB74586}', []) then
begin
ADOTable1.Edit;
ADOTable1.FieldByName('facilityaddress1').AsString:='mickey street';
ADOTable1.Properties.Item['Update Criteria'].Value:=adCriteriaKey;
// **B**
ADOTable1.Post;
end
else
showmessage('not found!');
ADOTable1.Close;
conMain.Close;
end;
就好像Post方法或连接使数据库处于某种中间状态......
这是我演示问题1时数据库日志所说的内容。
M@D3400 on db
SET NAMES latin1
SET character_set_results = NULL
SET SQL_AUTO_IS_NULL = 0
select database()
select database()
SHOW GLOBAL STATUS
SELECT @@tx_isolation
set @@sql_select_limit=DEFAULT
select * from facility
SHOW KEYS FROM `facility`
UPDATE `db`.`facility` SET `FacilityAddress1`=? WHERE `facilityid`=?
UPDATE `db`.`facility` SET `FacilityAddress1`='mickey street22' WHERE `facilityid`='{C0FADCC8-15C9-48C8-8003-3BBD4AB74586}'
这是我演示问题2时数据库日志所说的内容。
SHOW GLOBAL STATUS
M@D3400 on db
SET NAMES latin1
SET character_set_results = NULL
SET SQL_AUTO_IS_NULL = 0
select database()
select database()
SELECT @@tx_isolation
set @@sql_select_limit=DEFAULT
select * from facility
SHOW KEYS FROM `facility`
UPDATE `db`.`facility` SET `FacilityAddress1`=? WHERE `facilityid`=?
// SUCCESSFUL
UPDATE `db`.`facility` SET `FacilityAddress1`='mickey street22' WHERE `facilityid`='{C0FADCC8-15C9-48C8-8003-3BBD4AB74586}'
SHOW GLOBAL STATUS
SHOW GLOBAL STATUS
SHOW GLOBAL STATUS
db
select * from facility
UPDATE `db`.`facility` SET `FacilityAddress1`=? WHERE `facilityid`=?
// ERROR!
UPDATE `db`.`facility` SET `FacilityAddress1`='mickey street22' WHERE `facilityid`='{C0FADCC8-15C9-48C8-8003-3BBD4AB74586}'
SHOW GLOBAL STATUS
设置&#34;更新标准&#34;在各个地方都没有帮助。
将表格缩小为两个字段:facilityid varchar(38),facilityaddress1 varchar(50)。同样的结果......
答案 0 :(得分:0)
表格http://www.connectionstrings.com/mysql-connector-odbc-5-2/,我发现:
Provider = MSDASQL; Driver = {MySQL ODBC 5.3 UNICODE Driver}; Persist Security Info = True; Server = yukon; Database = ocean; User = M; Password = p; Option = 2; 强>
建议VB使用“Option = 2” http://dev.mysql.com/doc/connector-odbc/en/connector-odbc-configuration-connection-parameters.html#codbc-dsn-option-combos
作品!的
谢谢大家的贡献。