How to insert cell array of double in to database table

I am trying to insert cell array that contains double data.how to insert mxn double data into database table field. For example, the sample code is
tablename = 'LabelData1';
data = ([346565774,4637857]);
data = table(data);
sqlwrite(conn,tablename,data,'ColumnType',{"double"})
but i am getting folling error
_ _Error using database.odbc.connection/sqlwrite (line 102)
ODBC JDBC/ODBC Error: ODBC Driver Error: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ')'..__
I am using MS SQL server

 Réponse acceptée

Kojiro Saito
Kojiro Saito le 26 Juin 2018
Modifié(e) : Kojiro Saito le 28 Juin 2018
Since double is not a valid data type in SQL Server, you need to use numeric instead. Also, you need to insert each data to each column of SQL Server. The following will work.
tablename = 'LabelData1';
data = table(346565774, 4637857);
sqlwrite(conn,tablename,data,'ColumnType', ["numeric" "numeric"])
UPDATED
I don't think it is possible to insert multiple numeric values in a single column, but there's a workaround. The following codes insert multiple values as a character.
tablename = 'LabelData1';
data = ["97.877049180327900,67.666276346604180,1.669320843091335e+02,1.247775175644028e+02"];
data = table(data);
sqlwrite(conn,tablename,data,'ColumnType', "varchar(255)")
The data will be inserted in one column as varchar.
After that, you can read from this table and convert the data to double to use it in your another processing.
data2 = sqlread(conn, tablename);
data2 = data2.data;
data2 = sscanf(data2{:}, '%f,%f,%f,%f');

3 commentaires

Actually I need to insert mxn double data into a single filed. For example
tablename = 'LabelData1';
data = ([97.877049180327900,67.666276346604180,1.669320843091335e+02,1.247775175644028e+02]);
data = table(data);
sqlwrite(conn,tablename,data,'ColumnType',{"numeric"})
but I am getting following error
Error using database.internal.utilities.TypeMapper.dataTypeConverter (line 139)
data column value must be a numeric array or cell array of numeric scalars.
can you help on this issue?.
OK, please see my updated answer.
I have the same problem.
The fact is that I already have a database in Access and the numbers I want to insert are already doubles. (The solution must not be changing my database into varchars).
When using sqlwrite, my numbers are truncated in the seventh digit.
1.234.567,64987 is inserted as 1.234.570,00!!!
Is there any way of instering doubles in an existing database?
Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by