1

插入 sqlite3 数据库时出现错误“由于违反约束列 transid 不唯一而中止”。这是我的代码,有人可以帮助我吗?

Public Connection As SQLiteConnection
Public Function Getconnection() As SQLiteConnection
    Connection = New SQLiteConnection
    Connection.ConnectionString = "Data Source=" + Application.StartupPath + "\POS.s3db; Version=3;"
    Connection.Open()
    GetConnection = Connection
End Function

Using cmd As New SQLiteCommand(strSQL,Connection)
            cmd.CommandType = CommandType.Text
            strSQL = "insert into tbltrans2 (transid,itemcode,flddate,itemname,qty,price,total,btw,btwper) values ('@tid',@itemcode,'@date','@itemname','@qty','@price','@total','@btw',@btwper)"
            cmd.Parameters.AddWithValue("tid", txtTransId.Text)
            cmd.Parameters.AddWithValue("date", txtDate.Text)
            ''this is temp

            For Each ls As ListViewItem In ListItems.Items
                cmd.Parameters.AddWithValue("itemcode", ls.Tag)
                cmd.Parameters.AddWithValue("itemname", ls.SubItems(0).Text)
                cmd.Parameters.AddWithValue("qty", ls.SubItems(1).Text)
                cmd.Parameters.AddWithValue("price", ls.SubItems(2).Text)
                cmd.Parameters.AddWithValue("total", ls.SubItems(3).Text)
                cmd.Parameters.AddWithValue("btw", (Double.Parse(ls.SubItems(5).Text) / 100) * (Double.Parse(ls.SubItems(3).Text)).ToString("#,##0.00"))
                cmd.Parameters.AddWithValue("btwper", Double.Parse(ls.SubItems(5).Text))
                cmd.ExecuteNonQuery()
            Next ls
        End Using
4

1 回答 1

1

看起来有一个unique约束transid。这意味着您不能插入transid表中已经存在的行。

尝试输入不同的transid,或设置transid自动增量列,这样您根本不必输入它。

于 2012-11-25T10:57:48.630 回答