2012-12-18 107 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

在我的问题最底部看看我的评论。我的应用程序检查模式中的所有表是否阻止使用损坏的数据库文件等工作 –