Name: Anonymous 2008-05-15 11:39
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SQLiteConnection.CreateFile("failure.db");
SQLiteConnection.CompressFile("failure.db");
SQLiteConnection DB = new SQLiteConnection("Data source=failure.db");
DB.Open();
using (SQLiteCommand DBc = DB.CreateCommand())
{
DBc.CommandText = "CREATE TABLE 'records' (" +
"'id' text, " +
"'name' text, " +
"'address' text, " +
"'phone' text)";
DBc.ExecuteNonQuery();
}
using (SQLiteCommand DBc = DB.CreateCommand())
{
DBc.CommandText = "INSERT INTO 'records' " +
"(id, name, address, phone) VALUES " +
"('js', 'john', 'lolwut', '92837412')";
DBc.ExecuteNonQuery();
}
using (SQLiteCommand DBc = DB.CreateCommand())
{
DBc.CommandText = "UPDATE 'records' SET ? = ? WHERE id = ?";
SQLiteParameter prm1 = DBc.CreateParameter();
SQLiteParameter prm2 = DBc.CreateParameter();
SQLiteParameter prm3 = DBc.CreateParameter();
DBc.Parameters.Add(prm1);
DBc.Parameters.Add(prm2);
DBc.Parameters.Add(prm3);
prm1.Value = "address";
prm2.Value = "dickbutt";
prm3.Value = "js";
DBc.ExecuteNonQuery();
}
using (SQLiteCommand DBc = DB.CreateCommand())
{
DBc.CommandText = "SELECT * FROM 'records'";
using (SQLiteDataReader DBr = DBc.ExecuteReader())
{
DBr.Read();
Console.WriteLine(" ID> " + DBr.GetString(0));
Console.WriteLine("NAME> " + DBr.GetString(1));
Console.WriteLine("ADDR> " + DBr.GetString(2));
Console.WriteLine(" TEL> " + DBr.GetString(3));
}
}
Console.Read();
}
}
}
It crashes when it executes the update query. Error message reads "SQLite error near "?": syntax error".
It's worth to notice that it works perfectly if I comment out prm1 and enter the column name directly into the CommandText.
What am I doing wrong?
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SQLiteConnection.CreateFile("failure.db");
SQLiteConnection.CompressFile("failure.db");
SQLiteConnection DB = new SQLiteConnection("Data source=failure.db");
DB.Open();
using (SQLiteCommand DBc = DB.CreateCommand())
{
DBc.CommandText = "CREATE TABLE 'records' (" +
"'id' text, " +
"'name' text, " +
"'address' text, " +
"'phone' text)";
DBc.ExecuteNonQuery();
}
using (SQLiteCommand DBc = DB.CreateCommand())
{
DBc.CommandText = "INSERT INTO 'records' " +
"(id, name, address, phone) VALUES " +
"('js', 'john', 'lolwut', '92837412')";
DBc.ExecuteNonQuery();
}
using (SQLiteCommand DBc = DB.CreateCommand())
{
DBc.CommandText = "UPDATE 'records' SET ? = ? WHERE id = ?";
SQLiteParameter prm1 = DBc.CreateParameter();
SQLiteParameter prm2 = DBc.CreateParameter();
SQLiteParameter prm3 = DBc.CreateParameter();
DBc.Parameters.Add(prm1);
DBc.Parameters.Add(prm2);
DBc.Parameters.Add(prm3);
prm1.Value = "address";
prm2.Value = "dickbutt";
prm3.Value = "js";
DBc.ExecuteNonQuery();
}
using (SQLiteCommand DBc = DB.CreateCommand())
{
DBc.CommandText = "SELECT * FROM 'records'";
using (SQLiteDataReader DBr = DBc.ExecuteReader())
{
DBr.Read();
Console.WriteLine(" ID> " + DBr.GetString(0));
Console.WriteLine("NAME> " + DBr.GetString(1));
Console.WriteLine("ADDR> " + DBr.GetString(2));
Console.WriteLine(" TEL> " + DBr.GetString(3));
}
}
Console.Read();
}
}
}
It crashes when it executes the update query. Error message reads "SQLite error near "?": syntax error".
It's worth to notice that it works perfectly if I comment out prm1 and enter the column name directly into the CommandText.
What am I doing wrong?