mardi 10 mars 2015

SQLite Table That Auto Increments Primary Key

Well the main problem is that I get a error when trying to insert a Data Entry.


(Not auto incrementing the primary key)


Table Creation:



SQLiteConnection _sqliteCon = new SQLiteConnection(_connectStr);
_sqliteCon.Open();

string query5 = "CREATE TABLE Merkmal(MerkmalID INT PRIMARY KEY NOT NULL, ";
query5 += "MerkmalName_FR TEXT);";
SQLiteCommand command5 = new SQLiteCommand(query5, _sqliteCon);
command5.ExecuteNonQuery();
_sqliteCon.Close();


This appears to work fine.


Now for the Insert: (I Assume this is where my mistake is occuring )



SQLiteConnection _sqliteCon = new SQLiteConnection(_connectStr);
_sqliteCon.Open();
SQLiteCommand insertSQL = new SQLiteCommand("INSERT INTO Merkmal(MerkmalID,MerkmalName_FR) VALUES (?,?)",_sqliteCon);
insertSQL.Parameters.Add(new SQLiteParameter("param1",null));
insertSQL.Parameters.Add(new SQLiteParameter("param2",SomeString));
insertSQL.ExecuteNonQuery();
_sqliteCon.Close();


Some things worth noting:




  1. I have attempted not putting a value at all. (With hardcoded values)




  2. I get the not null exception error when trying to insert.




What is the correct way to create or insert into a table so that it auto increments the Primary Key


Thanks in Advance guys.


Aucun commentaire:

Enregistrer un commentaire