使用自动增量列

时间:2014-04-10 18:49:44

标签: c# sqlite ado.net

我有12列,第一列是主键和自动增量。所以在我的插入命令中,我传递了11个值,但我一直得到错误说,表有12列,但提供了11个值。

这是create table语句:

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#似乎有所帮助,但没有奏效。我做错了什么?

我做错了什么?

使用insert语句时,除了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 ();
}

2 个答案:

答案 0 :(得分:2)

您案例中的正确插入查询如下所示:

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

如果并非所有列都在插入中受影响,则必须显式登记所有列。省略列列表意味着必须插入所有列。

答案 1 :(得分:1)

Hamlet Hakobyan有一个解决方案,但还有另一种方法:

如果您在NULL列中插入INTEGER PRIMARY KEY row id autogeneration kicks in。因此,请保留代码并在此处将NULL添加到SQL:

String insertString = "INSERT INTO coupons VALUES(NULL, @title, @description, @imagepath, @couponpath, @barcodepath, @downloadedon, @redeemedon, @starttime, @endtime, @status, @statustext)";