SQLite的 TEXT类型 是不是 可以直接插入 string类型? 仿照下面的例子后! 发现提示 insert into语句的语法错误
我的     string sql = "insert into YTC(HC)values(@Half)";
我确实建立了 YTC这个表  并且也有 HC这列  这列的类型 我建的时候选的TEXT类型 
public void InsertLevel1()
        {
            string sql = "insert into category(id,parentId,name,displayOrder)values(@id,@parentId,@name,@displayOrder)";
            using (SQLiteConnection connection = new SQLiteConnection("Data Source=D:\\category.db"))
            {
                connection.Open();
                using (DbTransaction transaction = connection.BeginTransaction())
                {
                    using (SQLiteCommand command = new SQLiteCommand(sql, connection))
                    {
                        command.Parameters.Add(new SQLiteParameter("@id", DbType.Int32));
                        command.Parameters.Add(new SQLiteParameter("@parentId", DbType.Int32));
                        command.Parameters.Add(new SQLiteParameter("@name", DbType.String));
                        command.Parameters.Add(new SQLiteParameter("@displayOrder", DbType.Byte));
                        for (int i = 1; i <= Level1; i++)
                        {
                            command.Parameters["@id"].Value = i;
                            command.Parameters["@parentId"].Value = 0;
                            command.Parameters["@name"].Value = string.Format("Level_{0}_Name_{1}", i, i);
                            command.Parameters["@displayOrder"].Value = random.Next(1, 256);
                            command.ExecuteNonQuery();
                        }
                    }
                    transaction.Commit();
                }
            }
        }

解决方案 »

  1.   

    string sql = "insert into YTC(HC) values(@Half)";
    少个空格
      

  2.   

    string sql = "insert into category(id,parentId,name,displayOrder)values("+@id+",@parentId,@name,'"+@displayOrder+"')";注引加引号,如果是varchar型需加单引号,如最后一个参数
      

  3.   

     command.Parameters["@displayOrder"].Value = random.Next(1, 256);
    =========
     command.Parameters["@displayOrder"].Value = (byte)random.Next(1, 256);random.Next(1, 256)返回的是int类型
      

  4.   

    楼主不能跟踪一下吗,看看执行的sql语句到底是什么样子的
      

  5.   

    你这样直接插,这个YTC表的其他列可以为空吗?你只插入这一列的值,可能和表结构冲突了。
      

  6.   

    --------
    我是仿照这个人家给我的例子写的!
    -------------
    我自己的是这样的
                string sql = "insert into YTC(HC)values(@Half)";
                SQLiteConnection connection = new SQLiteConnection(@"Data Source=d:/我的文档/Visual Studio 2008/Projects/Em/Em/bin/Debug/Test.db3");
                connection.Open();
                DbTransaction transaction = connection.BeginTransaction();
                SQLiteCommand command = new SQLiteCommand(sql, connection);
                command.Parameters.Add(new SQLiteParameter("@Half", DbType.String));
                command.Parameters["@Half"].Value = Value;
                command.ExecuteNonQuery();
                command.Dispose();
               connection.Close();
               connection.Dispose();
      

  7.   


    --------------
    可以啊 我看了下 NULL属性里 是打的YES
      

  8.   

    ing sql = "insert into YTC(HC) values(@Half)";
                                   ——
    还是没空格Value 为多少?
      

  9.   

    command.Parameters.Add(new SQLiteParameter("@Half", DbType.String)).Value = Value;这样试试
      

  10.   

    Value  我赋值了 一个String  类型
      

  11.   

    -----
    这样不行 我看的报错 insert into语句的语法错误
      

  12.   

    语法错误就检查SQL语句就行了
      

  13.   

    @Half 你怎么写的 要么就你就发个全的
      

  14.   

    @Half  要怎么写 我不知道 这个是怎么写的!我所有的代码都贴出来了!
      

  15.   

    @Half  我一直以为这个是类似于变量的东西!
      

  16.   

    我的意思是  是不是你@Half传的时候出错了,语法错误肯定就是SQL语句错误
      

  17.   

      command.Parameters.Add(new SQLiteParameter("@Half", DbType.String));
      command.Parameters["@Half"].Value = Value;您是说这两句吗:?
      

  18.   

    Level1  这个变量是多少啊,有没有进这个循环还不一定呢,你调试看看有没有进入这个循环,如果没有进入这个循环的话,你的当然会报那个错误啦
      

  19.   

    @displayOrder  看看这个数据带进去得到的sql语句是什么 然后到数据库执行一下 如果数据库执行不成功的话 你就知道问题在哪里了
      

  20.   

    ------------------
    @displayOrder  不太明白 您的意思
      

  21.   

    SQLite error
    near "insert into YTC": syntax error
      

  22.   

    insert into YuanTongContours(HalfContours)  value('dsdasdasd') 发现点问题了 
    在SQLite Manager 里面 执行 这条SQL语句 都不行!
      

  23.   

    我数据库里 创建的 是TEXT类型!
      

  24.   

    似乎发现点问题了
    SQLite里的语句 和其他的数据库 似乎不太一样
    INSERT INTO "YTC" VALUES(55, 'This is sample data', 'lala');表的名字需要加双引号
    不知道这种在C#里怎么写!
      

  25.   


    恩,一般就是单步执行,检查一下你需要的sql字符串是否有问题,空格呀,关键字呀,都要注意的哈。
    检查一下你insert 的各个字段是否正确,字段数和values值的个数要相等。
      

  26.   

    搞的快要崩溃了  整了一晚上!
    谁能给个简单的例子插入 传给我 不胜感激!!!!
    [email protected]
      

  27.   

                string sql = "INSERT INTO [YTC] ([ID]) VALUES(@ID)";
              SQLiteConnection connection = new SQLiteConnection(@"Data Source=d:/我的文档/Visual Studio 2008/Projects/WriteConoursToDB/WriteConoursToDB/bin/Debug/NewDB.sqlite");
              connection.Open();
              SQLiteTransaction transaction = connection.BeginTransaction();
              SQLiteCommand command = new SQLiteCommand(sql, connection);
              SQLiteParameter myparam = new SQLiteParameter("@ID",DbType.Int32);
              command.Parameters.Add(myparam);
              myparam.Value = 34;
              command.Parameters["@ID"].Value = 4587;
              command.ExecuteNonQuery();
           
              command.Dispose();
              connection.Close();
              connection.Dispose();
              transaction.Dispose();
    试了好几种方法 不会报 INSErT语句错误了,但是就是没法存进去! 和无奈啊!!!
      

  28.   

    我没接触过SQLite,不过以我操纵别的数据库的经验,认为:
    你在connection.Close();之前,少了句transaction.Commit();另外:
    myparam.Value = 34;
      command.Parameters["@ID"].Value = 4587;
    会导致成功时被插入的值,是4587,而不是34
      

  29.   

              string sql = "INSERT INTO [YTC] ([ID],[Contours],[HalfContours]) VALUES(@ID,@C,@H)";
              SQLiteConnection connection = new SQLiteConnection(@"Data Source=d:/我的文档/Visual Studio 2008/Projects/WriteConoursToDB/WriteConoursToDB/bin/Debug/NewDB.sqlite");
              connection.Open();
              SQLiteTransaction transaction = connection.BeginTransaction();
              SQLiteCommand command = new SQLiteCommand(sql, connection);
              command.Parameters.Add(new SQLiteParameter("@ID", DbType.Int32));
              command.Parameters.Add(new SQLiteParameter("@C", DbType.String));
              command.Parameters.Add(new SQLiteParameter("@H", DbType.String)); 
              command.Parameters["@ID"].Value = 4587;
              command.Parameters["@C"].Value = Value;
              command.Parameters["@H"].Value = Value;
              command.ExecuteNonQuery();
              transaction.Commit();
              command.Dispose();
              connection.Close();
              connection.Dispose();
              transaction.Dispose();
    上传一个 可以插入的版本,网上很多版本都是不行的! 这样大家以后碰到类似的问题 ,可以直接仿照我的例子了!也不枉我花费了一个晚上的时间,搞这个简单的问题!