Got error export data from workspace into database

1 visualización (últimos 30 días)
Amir Hamzah UTeM
Amir Hamzah UTeM el 10 de Abr. de 2011
Editada: Walter Roberson el 2 de Jun. de 2015
How can i insert data from workspace into database?this is my code.
tableP=[0.02 0.01;0.02 0.01;0.02 0.01;0.02 0.01]; %my data
h = actxserver('DAO.DBEngine.36');
database = 'C:\Users\tV CaLeR\Documents\MATLAB\Maisarah BITD\cm1\data.mdb';
db = h.OpenDatabase(database);
tbl_names = {'data1'};
sql = ['INSERT * INTO ' char(tbl_names(1))];
rs = db.OpenRecordset(sql);
[row,col] = size(rs.GetRows(1));
rs.MoveLast;
ile_wierszy = rs.RecordCount;
rs.MoveFirst;
first=tableP(:,1)
secnd=tableP(:,2)
insert(db,'data1',first,secnd)
rs.Close;
db.Close;
i have this following error,
??? Invoke Error, Dispatch Exception:
Source: DAO.Database
Description: Syntax error in INSERT INTO statement.
Help File: jeterr40.chm
Help Context ID: 4c577e
Error in ==> Untitled at 9
rs = db.OpenRecordset(sql);
please help me..

Respuestas (2)

Oleg Komarov
Oleg Komarov el 10 de Abr. de 2011
The correct expression for an INSERT is:
INSERT table_name
VALUES (value1, value2, ...)
or
INSERT table_name
SELECT field1, field2, ... FROM other_table
  1 comentario
Amir Hamzah UTeM
Amir Hamzah UTeM el 10 de Abr. de 2011
Editada: Walter Roberson el 2 de Jun. de 2015
I Have try other code to insert data to DB,but got same error: ??? Invoke Error, Dispatch Exception: Source: DAO.Database Description: Syntax error in INSERT INTO statement. Help File: jeterr40.chm Help Context ID: 4c577e Error in ==> getrecords at 11 invoke(db,'Execute',x);
what i'm doing wrong? can someone expert repair my code correctly? i'm lack of knowledge about DB..here my code
cn=actxserver('Access.application');
db=invoke(cn.DBEngine,'OpenDatabase', 'C:\Users\tV CaLeR\Documents\MATLAB\Maisarah BITD\DB\tutorial db tool\test.mdb');
rs=invoke(db,'OpenRecordset','Table1');
fieldlist=get(rs,'Fields');
ncols=get(fieldlist,'Count');
for c=1:double(ncols)
fields{c}=get(fieldlist,'Item',c-1);
resultset.columnnames{c}=get(fields{c},'Name');
end;
x='INSERT INTO Table1 (percent,value) VALUES (33,"abcdefg")';
invoke(db,'Execute',x);
invoke(rs,'Close');
invoke(db,'Close');
delete(cn);
Thanks Amir

Iniciar sesión para comentar.


Amir Hamzah UTeM
Amir Hamzah UTeM el 10 de Abr. de 2011
I Have try other code to insert data to DB,but got same error: ??? Invoke Error, Dispatch Exception: Source: DAO.Database Description: Syntax error in INSERT INTO statement. Help File: jeterr40.chm Help Context ID: 4c577e Error in ==> getrecords at 11 invoke(db,'Execute',x);
what i'm doing wrong? can someone expert repair my code correctly? i'm lack of knowledge about DB..here my code
cn=actxserver('Access.application');
db=invoke(cn.DBEngine,'OpenDatabase', 'C:\Users\tV CaLeR\Documents\MATLAB\Maisarah BITD\DB\tutorial db tool\test.mdb');
rs=invoke(db,'OpenRecordset','Table1');
fieldlist=get(rs,'Fields');
ncols=get(fieldlist,'Count');
for c=1:double(ncols)
fields{c}=get(fieldlist,'Item',c-1);
resultset.columnnames{c}=get(fields{c},'Name');
end;
x='INSERT INTO Table1 (percent,value) VALUES (33,"abcdefg")';
invoke(db,'Execute',x);
invoke(rs,'Close');
invoke(db,'Close');
delete(cn);
Thanks Amir

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by