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