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();
}
}
}
我的 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();
}
}
}
少个空格
=========
command.Parameters["@displayOrder"].Value = (byte)random.Next(1, 256);random.Next(1, 256)返回的是int类型
我是仿照这个人家给我的例子写的!
-------------
我自己的是这样的
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();
--------------
可以啊 我看了下 NULL属性里 是打的YES
——
还是没空格Value 为多少?
这样不行 我看的报错 insert into语句的语法错误
command.Parameters["@Half"].Value = Value;您是说这两句吗:?
@displayOrder 不太明白 您的意思
near "insert into YTC": syntax error
在SQLite Manager 里面 执行 这条SQL语句 都不行!
SQLite里的语句 和其他的数据库 似乎不太一样
INSERT INTO "YTC" VALUES(55, 'This is sample data', 'lala');表的名字需要加双引号
不知道这种在C#里怎么写!
恩,一般就是单步执行,检查一下你需要的sql字符串是否有问题,空格呀,关键字呀,都要注意的哈。
检查一下你insert 的各个字段是否正确,字段数和values值的个数要相等。
谁能给个简单的例子插入 传给我 不胜感激!!!!
[email protected]
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语句错误了,但是就是没法存进去! 和无奈啊!!!
你在connection.Close();之前,少了句transaction.Commit();另外:
myparam.Value = 34;
command.Parameters["@ID"].Value = 4587;
会导致成功时被插入的值,是4587,而不是34
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();
上传一个 可以插入的版本,网上很多版本都是不行的! 这样大家以后碰到类似的问题 ,可以直接仿照我的例子了!也不枉我花费了一个晚上的时间,搞这个简单的问题!