Reputation: 334
after reading a few other posts it seems like what I want may be possible, but at the moment I have an array of unsigned characters I wish to enter into a row of my table in sqlite. now I know sqlite uses UTF-8 encoding so I should I think be able to do this but I'm unsure.
at the moment my code doesn't work I have a table...
void PersonalDataBase::openUserDataBase()
{
int rc;
//table setup
string table = "CREATE TABLE IF NOT EXISTS USERS (" \
"_id INTEGER PRIMARY KEY NOT NULL," \
"USERNAME TEXT NOT NULL," \
"PASSWORD TEXT," \
"USERHASH TEXT NOT NULL," \
"EVAL TEXT NOT NULL," \
"NVAL TEXT NOT NULL," \
"PVAL TEXT," \
"QVAL TEXT," \
"DVAL TEXT);";
sqlite3_stmt *stmt;
sqlite3_initialize();
/* Open database */
//Note rc should be checked for errors
rc = sqlite3_open_v2("database/data/UsersDB.db", &userDB, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
//add table
rc = sqlite3_prepare_v2(userDB, table.c_str(), -1, &stmt, 0);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
after calling this my table is created but now inserting into it...
now for inserting into my table, I create a new entry and generate my unsigned char arrays e, d, n, p, q then....
string insertQuery = "INSERT INTO USERS (USERNAME,PASSWORD,USERHASH,EVAL,NVAL,PVAL,QVAL,DVAL) " \
"VALUES ('" + username + "', '" + password + "', ";
//set E
string temp1 = "";
for (int i = 0; i < e -> getLength(); i++)
{
temp1 += (char) e -> getByte(i);
}
insertQuery += temp1 + "', '";
//set N
temp1 = "";
for (int i = 0; i < n -> getLength(); i++)
{
temp1 += (char) n -> getByte(i);
}
insertQuery += temp1 + "', '";
//set P
temp1 = "";
for (int i = 0; i < p -> getLength(); i++)
{
temp1 += (char) p -> getByte(i);
}
insertQuery += temp1 + "', '";
//set Q
temp1 = "";
for (int i = 0; i < q -> getLength(); i++)
{
temp1 += (char) q -> getByte(i);
}
insertQuery += temp1 + "', '";
//set D
temp1 = "";
for (int i = 0; i < d -> getLength(); i++)
{
temp1 += (char) d -> getByte(i);
}
insertQuery += temp1 + "');";
//insert Key + User data into Table
sqlite3_prepare_v2(userDB, insertQuery.c_str(), -1, &stmt1, 0);
sqlite3_step(stmt1);
sqlite3_finalize(stmt1);
I build my query, however I'm pretty sure a part of my problem is my getByte() function returns an unsigned char, this value could be anything, it's ascii equivalent could be say ' or , characters that I think are messing up my query, my question is is there a way I can directly write my unsigned chars to my database? or should I create a function that takes them converts their 8bit to 7bit then work with chars? however this still doesn't solve my issue that it could be ' and , or others characters. anyone have any ideas on what I should do here?
Upvotes: 2
Views: 1532
Reputation: 180070
To avoid formatting problems, use parameters:
string insertQuery = "INSERT INTO USERS (USERNAME,PASSWORD,USERHASH,EVAL,NVAL,PVAL,QVAL,DVAL) "
"VALUES (?,?,?,?,?,?,?,?);";
err = sqlite3_prepare_v2(userDB, insertQuery.c_str(), -1, &stmt1, 0);
// check err
sqlite3_bind_text(stmt1, 1, username.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt1, 2, password.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt1, 3, e.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt1, 4, n.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt1, 5, p.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt1, 6, q.c_str(), -1, SQLITE_TRANSIENT);
// or as blob:
sqlite3_bind_blob(stmt1, 7, d.byte_ptr(), d.byte_count(), SQLITE_TRANSIENT);
err = sqlite3_step(stmt1);
Upvotes: 1