我使用System.Data.SQLite來處理我的數據庫。下面是我如何創建的表:我使用這個SQLite錯誤:沒有這樣的表
CREATE TABLE spare_samples (
sampleId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
sampleNr INTEGER UNSIGNED UNIQUE NOT NULL,
sampleName VARCHAR(255) UNIQUE NOT NULL COLLATE NOCASE,
spareState VARCHAR(255) NULL,
sides VARCHAR(255) NULL,
notes TEXT,
dispatch VARCHAR(32) NULL,
ebayId VARCHAR(255) NULL
);
CREATE TABLE spare_sample_photo_examples (
exampleId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
exampleType VARCHAR(32) NOT NULL,
exampleImage VARCHAR(128) UNIQUE NOT NULL COLLATE NOCASE,
exampleTitle VARCHAR(128) NULL,
exampleDescr TEXT,
exampleOrder INTEGER NOT NULL DEFAULT 0,
sampleId INTEGER NOT NULL,
FOREIGN KEY (sampleId)
REFERENCES spare_samples(sampleId)
ON UPDATE CASCADE ON DELETE CASCADE
);
要添加新行:
SQLiteConnection conn = new SQLiteConnection(LoadForm.connString);
SQLiteCommand command = conn.CreateCommand();
command.CommandText = "Insert Into spare_sample_photo_examples (exampleType, exampleImage, exampleTitle, exampleDescr, exampleOrder, sampleId) values('"
+ spareSamplePhotoExampleToUpdate.exampleType + "', '"
+ spareSamplePhotoExampleToUpdate.exampleImage + "', '"
+ spareSamplePhotoExampleToUpdate.exampleTitle + "', '"
+ spareSamplePhotoExampleToUpdate.exampleDescr + "', "
+ spareSamplePhotoExampleToUpdate.exampleOrder + ", "
+ spareSamplePhotoExampleToUpdate.sampleId + "); Select last_insert_rowid();";
Clipboard.SetText(command.CommandText);
try
{
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
spareSamplePhotoExampleToUpdate.exampleId = Convert.ToInt16(command.ExecuteScalar());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
finally
{
if (conn.State.ToString() != "Closed")
{
conn.Close();
}
}
而且spareSamplePhotoExampleToUpdate爲對象的實例:
public class SpareSamplePhotoExample
{
public int exampleId { get; set; }
public string exampleType { get; set; }
public string exampleImage { get; set; }
public string exampleTitle { get; set; }
public string exampleDescr { get; set; }
public int exampleOrder { get; set; }
public int sampleId { get; set; }
}
我不實際上我不知道這段代碼和我以及其他什麼有什麼區別,但是當我從應用生成代碼並在FF SQLite Manager中運行它時,它已成功執行。我也讀過另一個類似的問題,但他們都關心android和ios。
Insert Into spare_sample_photo_examples (exampleType, exampleImage, exampleTitle, exampleDescr, exampleOrder, sampleId) values('image', 'images\c438ldpuojpywln1.jpg', '', '', '0', '32'); Select last_insert_rowid();
非常感謝您的幫助。
評論:
在啓動此代碼檢查數據庫:
SQLiteCommand command = conn.CreateCommand();
command.CommandText = "SELECT count(name) FROM sqlite_master WHERE (type = 'table' AND name = 'cars')"
+ " OR (type = 'table' AND name = 'makes')"
+ " OR (type = 'table' AND name = 'models')"
+ " OR (type = 'table' AND name = 'spares')"
+ " OR (type = 'table' AND name = 'spare_brands')"
+ " OR (type = 'table' AND name = 'spare_samples')"
+ " OR (type = 'table' AND name = 'export_templates')"
+ " OR (type = 'table' AND name = 'users')"
+ " OR (type = 'table' AND name = 'user_meta')"
+ " OR (type = 'table' AND name = 'spare_sample_photo_examples')";
if (Convert.ToInt16(command.ExecuteScalar().ToString()) != 10)
{
//something like exit with some stuff
}
和明確的更新我的問題。正如你所看到的,我在數據庫中有另外一張表,所以在我開始添加照片示例之前,所有功能都能正常工作。出錯後,我無法做任何事,涉及數據庫操作。所有的操作都會返回相同的錯誤,但對於它們的表格。
我建議你看看參數化查詢:http://stackoverflow.com/questions/4141599/parameterized-queries-in-sqlite。曾聽說過SQL注入? – iamkrillin
你是如何運行CREATE TABLE查詢的? – PinnyM
您是否確認連接字符串中的數據庫位置和您在SQLite Manager中檢查的數據庫是否相同? – RandomEngy