Windows Phone中的SQLite客户端不一致错误

时间:2012-04-27 02:52:17

标签: c# database sqlite windows-phone-7 isolatedstorage

我尝试将数据插入数据库,但是当我选择插入的内容时,我会得到不一致的数据。

喜欢,我插入一个=" data1",b =" data2",c =" data3"

但是当我选择时,我得到一个=" 0",b =" data2",c =" data3"

为什么我使用相同的方法,但其中一些获得不同的价值..

我的工具参考:SQLiteClient for WP(我测试了他的代码,它运行正常,我完全遵循他是如何做到的,但后来我得到了不同的结果......)

INSERT功能:

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函数:

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;
}

我的UPDATE功能:

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);

感谢任何可以帮助我的人!!!

*或者如果您认为我应该放弃SQLite并转到其他数据库,请告诉我,我首先选择SQLite,因为它处理简单的查询,如mysql和FF SQLite Manager ..

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,我已经进行了测试并最终发现,我需要立即一起插入或更新所有列,然后只有我能正确完成工作,我不确定这是因为DBHelper限制还是来自SQLiteClient。

进一步详情:

例如,如果我的预设数据库声明如下4列:

[q_id, q_title, q_ques, q_ans] (q_id as Auto Increment)

然后当我执行INSERT或UPDATE时,即使我只需要插入q_title(同时保留带有DEFAULT值的q_ques和q_ans),我将需要在操作中包含所有列的参数:

INSERT INTO tlg_question (q_title,q_ques,q_ans) VALUES (@q_title,@q_ques,@q_ans)";

不像我在PHP MYSQL中通常做的那样,我可以简单地INSERT / UPDATE我喜欢的特定列而不会出现问题:

INSERT INTO tlg_question (q_title) VALUES (@q_title)";

p / s:我是SQLite和C#的新手,如果你知道实际发生了什么,请告诉/纠正我..

谢谢! :)