2

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 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
            }

そして、私の問題の決定的な更新。ご覧のとおり、データベースに別のテーブルがあるため、写真の例を追加する前に、すべての機能が適切に機能します。エラーが発生した後は、データベース操作を含む何もできません。すべての操作は同じエラーを返しますが、それらのテーブルに対してです。

4

2 に答える 2

1

答えが出ました!接続するデータベースの種類を選択できる接続オプション フォームがあります。パラメータの 1 つはパスです。次の方法で保存しました:

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

そのため、データベース ファイルが app フォルダー内にある場合、パスはよりきれいに見えました。また、フォームにはファイル ダイアログがあり、ここで写真の例を作成できます。あなたがすでに知っている次の話。アプリは、写真を開いたフォルダ内のデータベース ファイルの検索を開始しました。相対パスに注意してください。

于 2012-12-18T22:58:17.320 に答える
0

LoadForm.ConnString は正しいアカウントを使用していますか? sqlite マネージャーで 1 つの方法でログインし、1 つのスキーマでテーブルを作成しても、アプリが別のアカウント/スキーマでログインする場合、そのテーブルは現在のスキーマでアプリケーションによって検出されません。

于 2012-12-18T18:10:16.207 に答える