我使用 System.Data.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 管理器中运行它时,它已成功执行。此外,我还阅读了另一个类似的问题,但它们都涉及 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
}
并针对我的问题进行最终更新。如您所见,我在数据库中有另一个表,所以在我开始添加照片示例之前,所有功能都可以正常工作。出现错误后,我什么也做不了,这涉及到数据库操作。所有操作都返回相同的错误,但对于它们的表。