I'm starting a new project that has to convert data from XML
to db
. XMLs
have their own format and cannot be used to fill a db
.
I choose to use sqlite
, because it is an embedded platform and I need a lightweight library.
I'm struggling with columns types. I wrote the sql below:
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
static bool exec_sql (sqlite3 *db, char *sql, bool use_callback)
{
int rc;
char *zErrMsg = 0;
// Execute SQL statement
if (use_callback == true)
{
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
}
else
{
rc = sqlite3_exec(db, sql, NULL, 0, &zErrMsg);
}
if( rc != SQLITE_OK )
{
fprintf(stderr, "SQL: %s FAIL!!!\nError: %s\n", sql, zErrMsg);
sqlite3_free(zErrMsg);
return false;
}
return true;
}
int main ( int argc, char *argv[] )
{
xmlDoc *doc;
xmlNode *root_element;
FILE *fw;
int i, srcidx, next_start;
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
rc = sqlite3_open("Configuration.db", &db);
if( rc )
{
XML2DB_DEBUG_ERR("Can't open database: %s\n", sqlite3_errmsg(db));
}
else
{
XML2DB_DEBUG_INFO("Opened database successfully\n");
}
sql = "CREATE TABLE Table2(" \
"ID INT NOT NULL PRIMARY KEY," \
"TABLE_1_ID INT FOREGN KEY REFERENCES Table1(ID) NOT NULL ," \
"COLUMN_1 INT[512] NOT NULL,"\
"COLUMN_2 TEXT NOT NULL,"\
"COLUMN_3 BOOLEAN DEFAULT FALSE);";
// Execute SQL statement
exec_sql(db, sql, false);
sql = "INSERT INTO Models (ID, TABLE_1_ID, COLUMN_1, COLUMN_2, COLUMN_3) "\
"VALUES (1, 1, '{1,2,3,4,5}', 'blabla', TRUE); ";
// Execute SQL statement
exec_sql(db, sql,true);
sql = "SELECT * FROM Table2;";
// Execute SQL statement
exec_sql(db, sql, true);
sqlite3_close(db);
return 0;
}
That code works well, but now I found out that sqlite3
does not support boolean
and array
datatypes.
I wrote, "it works well", because of a SELECT
and DBVisualizer
can display data and do not give me errors.
So, the questions are:
SQLite uses dynamic typing .
To check data types, use CHECK constraints :
CREATE TABLE Table2(
ID INTEGER PRIMARY KEY,
TABLE_1_ID INT FOREIGN KEY REFERENCES Table1(ID) NOT NULL,
COLUMN_1 INT[512] NOT NULL
COLUMN_2 TEXT NOT NULL CHECK (typeof(COLUMN_2) = 'text'),
COLUMN_3 BOOLEAN DEFAULT 0 CHECK (COLUMN_3 IN (0, 1))
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.