I am following a youtube video that makes a basic C# console app that will create and write to a SQLite database. The code in the video executes but I am getting an unhandled exception that states that {"SQL logic error or missing database\r\ntable Mytable has no column named Name"}
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
namespace SQLiteProject
{
class Program
{
static void Main(string[] args)
{
string createQuery = @"CREATE TABLE IF NOT EXISTS
[Mytable] (
[Id} INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[Name] NVARCHAR(2048) NULL,
[Gender] NVARCHAR(2048) NULL)";
System.Data.SQLite.SQLiteConnection.CreateFile("sample.db3");
using(System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=sample.db3"))
{
using(System.Data.SQLite.SQLiteCommand cmd=new System.Data.SQLite.SQLiteCommand(conn))
{
conn.Open();
cmd.CommandText = createQuery;
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO Mytable(Name,Gender) values('Brett','male')";
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT * from Mytable";
using(System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader["Name"]+ ":" + "reader['Gender']");
}
conn.Close();
}
}
}
Console.ReadLine();
}
}
}
Typo in the CREATE TABLE area caused an issue with the brackets and curly braces. Section should read:
string createQuery = @"CREATE TABLE IF NOT EXISTS
[Mytable] (
[Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[Name] NVARCHAR(2048) NULL,
[Gender] NVARCHAR(2048) NULL)";