【问题标题】:Using auto increment column使用自动增量列
【发布时间】: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 方法更新了这个问题。请看一看。谢谢...

标签: c# sqlite ado.net


【解决方案1】:

在您的情况下,正确的插入查询如下所示:

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

如果不是所有列都在插入中受影响,您必须明确列出所有列。省略列列表意味着您必须插入所有列。

【讨论】:

  • 谢谢,我知道语法,但是,我正在尝试使用参数化方法并将某些列声明为自动增量和主键,应该自动插入值
  • 你试过我的建议了吗?仔细看看我写的东西。您不能显式插入自动增量列,因此您必须列出必须显式插入的所有列,期望自动增量一。
  • 哦好吧..会试试..然后回来
【解决方案2】:

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

【讨论】:

  • 这也以同样的方式工作,但现在我在使用 Hamlet 和你的方式的自动增量字段中看不到任何值。这是为什么呢?
  • @VamsiChalla 你是什么意思?这是一个主键字段,所以它必须有一个值。
  • 我将输出添加到问题中。请看一看。我试图显示表格中的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 2019-05-11
  • 2011-12-05
相关资源
最近更新 更多