想問一個update問題? |
尚未結案
|
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
已經在txt切好兩個單子了,在資料表text1,兩個欄位test_string,test_count,在test_string,輸入幾個跟txt切好的單子一樣的,想做累加的動作,已經做好累加了,可是無法update進去,請高手幫幫忙。
var
i,n,t,e,g,r,cou,c_n:integer;
a,l:array[0..100]of string;
s,st_a,st_b,st_c,st_d:string;
begin adoConnection1.ConnectionString:='provider=microsoft.jet.oledb.4.0;data source=c:\test\db1.mdb;persist security info=false';
adoConnection1.LoginPrompt:=false;
adoConnection1.Connected:=true;
adoquery1.Connection:=adoconnection1;
adoquery1.Close;
st_d:='c:\test.txt';
memo1.Lines.LoadFromFile(st_d);
s:=memo1.Lines.Text;
i:=1;
while pos(',',s)>0 do
begin //(2)
a[i]:=copy(s,1,pos(',',s)-1
memo2.Lines.Add(a[i]);
s:=copy(s,pos(',',s) 1,length(s));
i:=i 1;
end; //(2)
t:=i;
i:=1;
for n:=1 to t do
begin
r:=1;
g:=(length(a[n])div 2)-1;
for e:=1 to g do
begin
l[e]:=copy(a[i],r,4);
memo3.Lines.Add(l[e]);
adoquery1.sql.text:='Select * from test1 where test_string ="' l[e] '"';
adoquery1.Active:=true;
While Not ADOQuery1.Eof do
begin
c_n:=adoquery1.FieldByName('test_count').AsInteger 1;
adoquery1.SQL.text:='update test1 set test_string:=c_n';
showmessage(l[e] '=' inttostr(c_n));
end; r:=r 2;
end;
i:=i 1;
end;
end;
|
瘋狂火星人
一般會員 發表:1 回覆:29 積分:6 註冊:2005-06-17 發送簡訊給我 |
|
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
adoquery1.sql.text:='Select * from test1 where test_string ="' l[e] '"';//比對有沒有一樣
adoquery1.Active:=true;
adoquery1.first;
While Not ADOQuery1.Eof do
begin //(5)
adoquery1.next;
c_n:=adoquery1.FieldByName('test_count').AsInteger 1;
adoquery1.SQL.text:=('update test1settest_count=" c_n "');
showmessage(l[e] '=' inttostr(c_n));
我想做的功能是l[e]這變數的字串,跟資料表裡的test_string比對,有沒有一樣,有的話,就在'test_count' 1,如果沒有的話,就新增在test_string,可是目前做的比對,已經可以做到累加了,也能夠SHOWMESSAGE上,但在資料表裡沒反應,或者,有高手能提供更好的辦法解決。
原本我還在想說用 edit1.Text:=inttostr(c_n); adoquery1.FieldByName('test_count').Value:=strtoint(edit1.Text);
但這樣也會有錯
|
瘋狂火星人
一般會員 發表:1 回覆:29 積分:6 註冊:2005-06-17 發送簡訊給我 |
>adoquery1.sql.text:='Select * from test1 where test_string ="' l[e] '"';//比對有沒有一樣
>adoquery1.Active:=true;
>adoquery1.first;
>While Not ADOQuery1.Eof do
>begin //(5)
>adoquery1.next;
>c_n:=adoquery1.FieldByName('test_count').AsInteger 1;
>adoquery1.SQL.text:=('update test1settest_count=" c_n "');
>showmessage(l[e] '=' inttostr(c_n)); 這裡面並沒有看到update test_string的東西..... >adoquery1.Active:=true;
>While Not ADOQuery1.Eof do
>begin
>c_n:=adoquery1.FieldByName('test_count').AsInteger 1;
>adoquery1.SQL.text:='update test1 set test_string:=c_n';
>showmessage(l[e] '=' inttostr(c_n));
>end; 第一篇發言裡面有個updat test1 set test_string:=c_n....
c_n如果是數字的話,型態要注意一下....
不過您不是要把l[e]裡面的東西新增到資料庫嗎??
怎麼沒有看到新增的程式..... >原本我還在想說用 edit1.Text:=inttostr(c_n); adoquery1.FieldByName('test_count').Value:=strtoint(edit1.Text); 印象中似乎是不能藉由修改query裡面欄位資料的方式來更動資料庫裡面的資料... 方便的話,能否提供您的資料表詳細設定,讓弟試試.... 發表人 - 瘋狂火星人 於 2005/09/01 21:00:41
|
瘋狂火星人
一般會員 發表:1 回覆:29 積分:6 註冊:2005-06-17 發送簡訊給我 |
將文字檔案逐行與資料庫現有的資料作比較,有重複的就將重複資料的test_count欄位 1,沒有重複的話就新增到資料庫裡面....
是要做這樣的功能嗎???
參考一下,只用一個memo物件和button,按下button之後,adoquery1專門作查詢,adoquery2作新增和修改
資料表的部分,我多了一個id欄位,由SQL自動產生,作為修改test_count的依據
id:int、test_str:char、test_count:int --------------------------------程式碼-------------------------------
var
i,cnt:integer;
begin
memo1.lines.loadfromfile('c:\test.txt')
//抓取memo1物件總行數,以迴圈逐行處理
for i:=0 to memo1.Lines.Count do
begin
//判斷memo1該行是否為空白字串,空白字串不做處理
if memo1.Lines.Strings[i]<>'' then
begin
adoquery1.SQL.Clear;
//查詢資料表
adoquery1.SQL.Add('select * from test');
//以memo1第i行的資料作條件
adoquery1.SQL.Add('where test_str=''' memo1.Lines.Strings[i] '''');
adoquery1.Open;
adoquery1.First;
//只要有查到資料
if adoquery1.FieldByName('test_count').AsString<>'' then
begin
//就從第一筆開始處理
while not adoquery1.Eof do
begin
//將該筆test_count欄位 1
cnt:=adoquery1.FieldByName('test_count').AsInteger 1;
adoquery2.SQL.Clear;
//更新資料
adoquery2.SQL.Add('update test set test_count=' inttostr(cnt));
//以id欄位作為判斷條件
adoquery2.SQL.Add('where id=' adoquery1.FieldByName('id').AsString);
adoquery2.ExecSQL;
adoquery1.Next;
end;
end
//如果memo1的第i行與資料庫現有資料無重複,就進行以下處理
else
begin
adoquery2.SQL.Clear;
//把memo1的第i行資料新增進去,test_count設為1
adoquery2.SQL.Add('insert into test');
adoquery2.SQL.Add('values(''' memo1.Lines.Strings[i] ''',1)');
adoquery2.ExecSQL;
end;
end;
end;
end;
---------------------------------------------------------------------- 發表人 - 瘋狂火星人 於 2005/09/01 23:46:25
|
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
|
瘋狂火星人
一般會員 發表:1 回覆:29 積分:6 註冊:2005-06-17 發送簡訊給我 |
|
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
|
ko
資深會員 發表:28 回覆:785 積分:444 註冊:2002-08-14 發送簡訊給我 |
paa 你好:
不會update的原因是紅色的那一段沒出現,
不過你不能用adoquery1來做動作要改用adoquery2
引言: While Not ADOQuery1.Eof do begin c_n:=adoquery1.FieldByName('test_count').AsInteger 1; adoquery1.SQL.text:='update test1 set test_string:=c_n'; adoquery1.execsql; showmessage(l[e] '=' inttostr(c_n)); end;
------
====================== 昏睡~ 不昏睡~ 不由昏睡~ |
瘋狂火星人
一般會員 發表:1 回覆:29 積分:6 註冊:2005-06-17 發送簡訊給我 |
引言: else begin adoquery2.SQL.Clear; // 把memo1的第i行資料新增進去,test_count設為1 adoquery2.SQL.Add('insert into test1'); adoquery2.SQL.Add('values(''' l[e] ''',1)'); adoquery2.ExecSQL; end; 我後面是想要insert,原來我的l[e],就是指一串字我把他分成二個字去做對比,可是怎麼會產生這個錯誤呢?為什麼要把一個字串分成兩個字串???? 感覺怪怪的...... 出現的錯誤是你上一篇回應的那張貼圖嗎?? 重新檢查你的SQL語法..... 或是你把程式詳細確實的功能寫出來,我在寫個程式你參考參考..... 發表人 - 瘋狂火星人 於 2005/09/02 20:33:59 |
paa
初階會員 發表:50 回覆:101 積分:30 註冊:2005-02-01 發送簡訊給我 |
我想要的功能是把txt裡的一串字,先用","分開成一行行的,之後在一行行中,兩個字給分開來,之後在資料庫裡尋找有沒有一樣的字,有的話,就累加,沒有的話,就insert,可是在insert裡,無法insert,可是跑時,卻沒錯,但沒反應。請高手幫幫我看一下,我把全部的程式碼貼給你看。
var
j,cnt:integer;
a,l:array[0..100]of string;
s,st_a,st_b,st_c,st_d:string;
i,n,t,e,g,r,cou,c_n:integer;
begin
adoConnection1.ConnectionString:='provider=microsoft.jet.oledb.4.0;data source=c:\test\db1.mdb;persist security info=false';
adoConnection1.LoginPrompt:=false;
adoConnection1.Connected:=true;
adoquery1.Connection:=adoconnection1;
adoquery2.Connection:=adoconnection1;
adoquery1.Close; st_d:='c:\test2.txt';
memo1.Lines.LoadFromFile(st_d); //顯示txt的字串在memo上
s:=memo1.Lines.Text;
i:=1; //,分段的行數
while pos(',',s)>0 do //傳回字串在字串中的位置
begin //(2)
a[i]:=copy(s,1,pos(',',s)-1);//切割一行行
memo2.Lines.Add(a[i]);// 顯示在memo上
s:=copy(s,pos(',',s) 1,length(s));//指,下一個字
i:=i 1; //陣列行數 1
end; //(2)
// 切割二個字
t:=i;//總數t
i:=1;//指第一個字
for n:=1 to t do //n是指第一筆,t是指最後一筆
begin //(3)
r:=1;//指每行的第一個字要放在for裡面,不然會無法指到每行的第一個字
g:=(length(a[n])div 2)-1;//把一個字二個byte變成一個byte
for e:=1 to g do //e每個字,g指每個字的最後一個
begin //(4)
l[e]:=copy(a[i],r,4);//每串字,取二個字放在陣列裡
memo3.Lines.Add(l[e]);//顯示每二個字 // 判斷memo1該行是否為空白字串,空白字串不做處理
if l[e]<>'' then
begin
adoquery1.SQL.Clear;
// 查詢資料表
adoquery1.SQL.Add('select * from test1');
// 以memo1第i行的資料作條件
adoquery1.SQL.Add('where test_str=''' l[e] '''');
adoquery1.Open;
adoquery1.First;
// 只要有查到資料
if adoquery1.FieldByName('test_count').AsString<>'' then
begin
// 就從第一筆開始處理
while not adoquery1.Eof do
begin
// 將該筆test_count欄位 1
cnt:=adoquery1.FieldByName('test_count').AsInteger 1;
adoquery2.SQL.Clear;
// 更新資料
adoquery2.SQL.Add('update test1 set test_count=' inttostr(cnt));
// 以id欄位作為判斷條件
adoquery2.SQL.Add('where id=' adoquery1.FieldByName('id').AsString);
adoquery2.ExecSQL;
adoquery1.Next;
end;
end
// 如果memo1的第i行與資料庫現有資料無重複,就進行以下處理
else
begin
adoquery2.SQL.Clear;
// 把memo1的第i行資料新增進去,test_count設為1
adoquery2.SQL.Add('insert into test1(test_str)');
adoquery2.SQL.Add('values(''' l[e] ''')');
showmessage(l[e]);
end;
end; r:=r 2; //指每個字的二個byte
end; //(4)
i:=i 1; //指字串累加
end; //(3) end;
|
瘋狂火星人
一般會員 發表:1 回覆:29 積分:6 註冊:2005-06-17 發送簡訊給我 |
如果memo1的第i行與資料庫現有資料無重複,就進行以下處理 else begin adoquery2.SQL.Clear; // 把memo1的第i行資料新增進去,test_count設為1 adoquery2.SQL.Add('insert into test1(test_str)'); ★adoquery2.SQL.Add('values(''' l[e] ''',' testcount ')'); adoquery2.ExecSQL; showmessage(l[e]); end;我哩咧..... 你的query語法下完之後,並沒有執行adoquery2.execsql... 這樣當然資料庫不會動囉...... 而且你的query語法並沒有包含test_count,這樣它會變成null.... 之後你萬一想把它 1,就會出現問題..... 把上面紅色的字加進去 如果加了.execsql下去一樣沒反應,在參考底下的..... 不曉得你的資料表實際的排列方式是如何???是用哪個牌子的資料庫?? 我在測試的時候,排列順序是id->test_str->test_count 你再新增的時候沒有發現錯誤訊息,但資料庫裡面沒有反應..... 那建議你先把新增這部分的SQL語法直接用資料庫本身的管理介面來實做看看語法正不正確...... 語法沒問題之後,在我上面打★號的地方設定中斷,程式執行到中斷點之後,直接用watch看adoquery2.sql.text的內容... 網頁回應比較慢,如果還是有問題的話,我的msn:abc.a670505@msa.hinet.net 鄙人不算高手,只要你不嫌棄,也很樂意跟網友切磋交流.... 發表人 - 瘋狂火星人 於 2005/09/05 22:04:26 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |