0

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

比如,我插入 a="data1", b="data2", c="data3"

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

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

我的工具参考:SQLiteClient for WP(我测试了他的代码,它运行正确,我完全按照他的做法,但是我得到了不同的结果..)

插入功能:

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

选择功能:

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

还有我的更新功能:

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..

谢谢!

4

1 回答 1

0

好的,我已经测试过了,最后发现,我需要同时插入或更新所有列,然后才能正确完成工作,我不确定这是因为 DBHelper 限制或 SQLiteClient 的限制。

更多详细信息:

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

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

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

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

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

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

p/s:我是 SQLite 和 C# 的新手,如果您知道实际发生的事情,请建议/纠正我..

谢谢!:)

于 2012-04-28T05:55:06.453 回答