2012-12-18 106 views
2

我使用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 
      } 

和明確的更新我的問題。正如你所看到的,我在數據庫中有另外一張表,所以在我開始添加照片示例之前,所有功能都能正常工作。出錯後,我無法做任何事,涉及數據庫操作。所有的操作都會返回相同的錯誤,但對於它們的表格。

+1

我建議你看看參數化查詢:http://stackoverflow.com/questions/4141599/parameterized-queries-in-sqlite。曾聽說過SQL注入? – iamkrillin

+0

你是如何運行CREATE TABLE查詢的? – PinnyM

+0

您是否確認連接字符串中的數據庫位置和您在SQLite Manager中檢查的數據庫是否相同? – RandomEngy

回答

1

我已經得到了答案!我有連接選項,可以在其中選擇要連接的數據庫類型。其中一個參數是路徑。我保存下一步:

fDialog.FileName.Replace(Path.GetDirectoryName(Application.ExecutablePath) + "\\", ""); 

因此,當數據庫文件在應用程序文件夾內時,路徑看起來更乾淨。在窗體中有文件對話框,我可以在其中創建照片示例。下一個故事你已經知道了。應用程序開始搜索文件夾中的數據庫文件,我在其中打開圖片。注意相對路徑。

0

您的LoadForm.ConnString是否使用正確的帳戶?如果您在sqlite管理器中登錄一種方式並在一個模式中創建表格,但您的應用程序使用不同的帳戶/架構登錄,那麼應用程序將無法在當前模式中找到該表格。

+0

在我的問題最底部看看我的評論。我的應用程序檢查模式中的所有表是否阻止使用損壞的數據庫文件等工作 –