无法成功解析mysql数据库信息

时间:2015-10-11 19:30:23

标签: mysql vb.net mysql-workbench wampserver

我试图在需要一些参数的模块中调用一个过程,因此:

MySQL code to be executed (extracted from the module's procedure):

Sub InsertRecord(ByVal tbl As String, ByVal cols() As String, ByVal params() As String, ByVal colCondition As String, ByVal paramCondition As String) 'As String

                :
                :

    comA.CommandText = "INSERT INTO " & tbl & " (" & newCols & ") SELECT * FROM (SELECT @params) AS tmp "
    comA.CommandText &= "WHERE NOT EXISTS (SELECT " & newCols & " FROM " & tbl & " WHERE " & colCondition & " = @param) limit 1"
    comA.Parameters.AddWithValue("@param", paramCondition)
    comA.Parameters.AddWithValue("@params", newParams)

                :
                :

End Sub

Call Code:

    myModule.SaveInfo("myprod", {"col_a", "col_b", "col_c", "col_d", "col_e", "col_f", "col_g", "col_h"}, _
                            {val_a, val_b, val_c, val_d, val_e, val_f, val_g, val_h}, "col_b", val_b)

提示:newColsnewParams(分别从cols()params()获取)是有问题的表的列的逗号分隔列表和要分别插入的值

当我运行我的代码时,我收到错误:column count doesn't match value count at row 1

请问我该如何解决这个问题?我已经有几天了。 提前谢谢。

修改
我决定在sub上编写代码,而不是使用模块。这是完整的子程序:

 Private Sub SaveProduct(ByVal p_code As String, ByVal b_id As String, ByVal m_id As String, _
                        ByVal c_id As String, ByVal des As String, ByVal o_det As String) 
    'modNewData.InsertRecord("products", {"prd_code", "brnd_id", "mnu_id", "cat_id", "prd_description", "prd_details", "prd_create_date", "prd_last_update"}, _
    '                        {p_code, b_id, m_id, c_id, des, o_det, Date.Now.ToShortDateString, Date.Now.ToShortDateString}, "brnd_id", b_id)

    con = New MySqlConnection("Server=localhost; Database=inventory_db; Uid=root; Pwd=;")
    comA = New MySqlCommand

    'INSERT NEW RECORD.
    comA.CommandText = "INSERT INTO products (prd_code, brnd_id, mnu_id, cat_id, prd_description, prd_details, prd_create_date, prd_last_update) "
    comA.CommandText &= "SELECT * FROM (SELECT @params) AS tmp "
    comA.CommandText &= "WHERE NOT EXISTS (SELECT prd_code, brnd_id, mnu_id, cat_id, prd_description, prd_details, prd_create_date, prd_last_update FROM products "
    comA.CommandText &= "WHERE brnd_id = @param) limit 1"
    comA.Parameters.AddWithValue("@param", b_id)
    comA.Parameters.AddWithValue("@params", {p_code, b_id, m_id, c_id, des, o_det, Date.Now.ToShortDateString, Date.Now.ToShortDateString})
    comA.Connection = con

    con.Open()
    comA.ExecuteNonQuery()
    con.Close()

    'Release used up components
    comA.Parameters.Clear()
    comA.Dispose()
    comA = Nothing
    con = Nothing

    MsgBox("done.")

此时,我似乎并不知道自己在做什么,但我知道自己想要达到的目标 - 在表格中插入新记录。 我坚信我的sql代码存在问题,但无法确切知道它是什么。此时非常需要您的帮助。提前谢谢。

编辑2: 我调整了上面的mysql insert code,现在我可以成功地将新记录插入到我的表中。但是还存在一个问题:我的<Unable to read data>datetime列分别出现timestamp错误。

id  prd_code    brnd_id    mnu_id   cat_id  desc       details    create_date (datetime)    last_update(timestamp)
10  PM-16326    10         9        6       Red        NULL       10/15/2013 8:22:00 AM     7/16/2015 9:13:03 AM
17  PM-55326    23         28       11      Olive oil  n/a        <Unable to read data>     <Unable to read data>

这里是新的插入代码:

    'INSERT NEW RECORD.
    comA.CommandText = "INSERT INTO products (prd_code, brnd_id, mnu_id, cat_id, prd_description, prd_details, prd_create_date, prd_last_update) "
    comA.CommandText &= "SELECT * FROM (SELECT @p_code, @b_id, @m_id, @c_id, @des, @o_det, '11/11/2015 9:00:00 AM', '11/12/2015 9:00:00 AM') AS tmp "
    comA.CommandText &= "WHERE NOT EXISTS (SELECT prd_code, brnd_id, mnu_id, cat_id, prd_description, prd_details, prd_create_date, prd_last_update FROM products "
    comA.CommandText &= "WHERE brnd_id = @b_id) limit 1"
    comA.Parameters.AddWithValue("@p_code", p_code)
    comA.Parameters.AddWithValue("@b_id", b_id)
    comA.Parameters.AddWithValue("@m_id", m_id)
    comA.Parameters.AddWithValue("@c_id", c_id)
    comA.Parameters.AddWithValue("@des", des)
    comA.Parameters.AddWithValue("@o_det", o_det)

如果我用date.now.tostring替换上面的两个日期,我会收到错误:you have repeated columns... 我真的需要你的帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

传递给过程的列(执行匹配)称为cols,但在查询中使用newCols

您获得的MySQL错误意味着您插入的值与您应该填写的列的数量不同:您不能将四个值插入五列或三列。

所以正在发生的事情显然是你从<{1}}中取出{em> 来取消newCols,在你省略的代码部分。可能,根据某些条件,您可以添加一些列(或留下一些列)。这就是导致你麻烦的原因。您应该同时将cols的值修改为其他值,可能是params

此外,可能条件并非总是如此,并且您只是偶尔观察到函数行为不端,或者通过了一些测试(因为该测试不会触发列修改错误)。