I tried to insert data into the database, but when I select what I’ve insert, I get inconsistent data.
like, I insert a=”data1″, b=”data2″, c=”data3″
but when I select I get a=”0″, b=”data2″, c=”data3″
why I use the same method but some of them get different value..
Reference for my implement: SQLiteClient for WP (I tested his codes it run correctly, and I follow exactly how he done it, but then I get different outcomes..)
INSERT function:
String t_title = txtTitle.Text;
String t_img = "test";
String t_answer_a = txtAnswer1.Text;
String t_answer_b = txtAnswer2.Text;
String t_answer_c = txtAnswer3.Text;
String t_answer_d = txtAnswer4.Text;
String t_answer = txtAnswer1.Text;
int t_cat = 1;
int rec;
Random rnd = new Random();
string strInsert = " INSERT INTO tlg_question (q_title,q_img,q_ques_a,q_ques_b,q_ques_c,q_ques_d,q_ans,q_cat) VALUES (@q_title,@q_img,@q_ques_a,@q_ques_b,@q_ques_c,@q_ques_d,@q_ans,@q_cat)";
tlg_question tst = new tlg_question
{
q_title = t_title,
q_img = t_img,
q_ques_a = t_answer_a,
q_ques_b = t_answer_b,
q_ques_c = t_answer_c,
q_ques_d = t_answer_d,
q_ans = t_answer,
q_cat = t_cat
};
rec = (Application.Current as App).db.Insert<tlg_question>(tst, strInsert);
SELECT function:
ObservableCollection<tlg_question> _tlg_questionEntries = null;
string strSelect = "SELECT q_id,q_title,q_img,q_ques_a,q_ques_b,q_ques_c,q_ques_d,q_ans,q_cat FROM tlg_question ORDER BY q_id DESC LIMIT 0,1";
_tlg_questionEntries = (Application.Current as App).db.SelectObservableCollection<tlg_question>(strSelect);
foreach (tlg_question data in _tlg_questionEntries)
{
qid = data.q_id;
txtTitle.Text += data.q_title;
img = data.q_img;
txtAnswer1.Text += data.q_ques_a;
txtAnswer2.Text += data.q_ques_b;
txtAnswer3.Text += data.q_ques_c;
txtAnswer4.Text += data.q_ques_d;
}
Also my UPDATE function:
String t_title = txtTitle.Text;
String t_answer_a = txtAnswer1.Text;
String t_answer_b = txtAnswer2.Text;
String t_answer_c = txtAnswer3.Text;
String t_answer_d = txtAnswer4.Text;
int rec;
Random rnd = new Random();
string strUpdate = " UPDATE tlg_question SET q_title=@q_title,q_ques_a=@q_ques_a,q_ques_b=@q_ques_b,q_ques_c=@q_ques_c,q_ques_d=@q_ques_d,q_ans=@q_ans WHERE q_id=@q_id";
tlg_question tst = new tlg_question
{
q_id = qid,
q_title = t_title,
q_ques_a = t_answer_a,
q_ques_b = t_answer_b,
q_ques_c = t_answer_c,
q_ques_d = t_answer_d,
q_ans = t_answer_a
};
rec = (Application.Current as App).db.Update<tlg_question>(tst, strUpdate);
Appreciate anyone who can help me out!!!
*Or if you think I should give up SQLite and go for other database, please let me know, I pick SQLite at first place because it handle simple query like mysql, and the FF SQLite Manager..
Thanks!
Ok, I’ve tested around and finally found that, I need to INSERT or UPDATE ALL COLUMNS together at once then only I can get the job done correctly, I’m not sure this happens because of the DBHelper restriction or from SQLiteClient.
Further details:
for example, if my preset database declared 4 columns as below:
then when I doing INSERT or UPDATE, even I just need to insert the q_title ONLY (while leave the q_ques and q_ans with DEFAULT values), I will need to include all columns’ parameters in the operation:
Not like what I usually doing in PHP MYSQL, I can simply INSERT/UPDATE the particular column I like without problem:
p/s: I’m new in SQLite and C#, please advise/correct me if you know whats actually happening..
Thanks! 🙂