【发布时间】:2014-04-10 18:49:44
【问题描述】:
我有 12 列,第一列作为主键和自动增量。所以在我的插入命令中,我传递了 11 个值,但我不断收到错误消息,表有 12 列,但提供了 11 个值。
这里是创建表语句:
public String CREATE_TABLE = "CREATE TABLE coupons"
+ "(sno INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "title VARCHAR(20), " +
"description VARCHAR(20), " +
"imagepath VARCHAR(20), " +
"couponpath VARCHAR(20), " +
"barcodepath VARCHAR(20), " +
"downloadedon VARCHAR(20), "
+ "redeemedon VARCHAR(20), " + "starttime VARCHAR(20), " + "endtime VARCHAR(20), "
+ "status VARCHAR(20), statustext VARCHAR(20))";
这是我的插入语句:
String insertString = "INSERT INTO coupons VALUES(@title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";
SqliteCommand command = new SqliteCommand (insertString, m_dbConnection);
command.Parameters.AddWithValue ("@title", title);
command.Parameters.AddWithValue ("@description", description);
command.Parameters.AddWithValue ("@imagepath", imagepath);
command.Parameters.AddWithValue ("@couponpath", couponpath);
command.Parameters.AddWithValue ("@barcodepath", barcodepath);
command.Parameters.AddWithValue ("@downloadedon", downloadedon);
command.Parameters.AddWithValue ("@redeemedon", redeemedon);
command.Parameters.AddWithValue ("@starttime", starttime);
command.Parameters.AddWithValue ("@endtime", endtime);
command.Parameters.AddWithValue ("@status", status);
command.Parameters.AddWithValue ("@statustext", statustext);
command.ExecuteNonQuery ();
How to increment an auto-increment field with ADO.NET in C# 似乎有帮助,但没有用。我做错了什么?
我做错了什么?
在使用插入语句时,您必须提及所有其他列名,除了 Autoincrement 列。
我现在的插入语句:
String insertString = @"INSERT INTO coupons(title, description, imagepath, couponpath, barcodepath, downloadedon, redeemedon, starttime, endtime, status, statustext) VALUES(@title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";
即使这样也有效:
String insertString = @"INSERT INTO coupons VALUES(NULL, @title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";
编辑:
现在错误消失了,但是当我从表中检索 sno 时,我没有看到任何值。
Sno: Title: Sprint Customer Feedback Campaign
如您所见,Sno 是空的。为什么会这样?
这是我从 db 读取数据的方式..
public void getDataFromTable ()
{
string selectString = "select * from coupons";
SqliteCommand command = new SqliteCommand (selectString, m_dbConnection);
SqliteDataReader reader = command.ExecuteReader ();
while (reader.Read ()) {
Console.WriteLine ("Sno: " + reader ["sno"] +
"\tTitle: " + reader ["title"] +
"\tDescription: " + reader ["description"] +
"\tImagePath: " + reader ["imagepath"] +
"\tCouponPath: " + reader ["couponpath"] +
"\tBarcodePath: " + reader ["barcodepath"] +
"\tDownloadedOn: " + reader ["downloadedon"] +
"\tRedeemedOn: " + reader ["redeemedon"] +
"\tStartTime: " + reader ["starttime"] +
"\tEndTime: " + reader ["endtime"] +
"\tStatus: " + reader ["status"] +
"\tStatusText: " + reader ["statustext"]
);
}
Console.ReadLine ();
}
【问题讨论】:
-
后续问题:
sno为空时如何取值? -
@laalto,我用我的 getDataFromTable 方法更新了这个问题。请看一看。谢谢...