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; }
}
나는 돈 여기에 내가 테이블을 만든 방법입니다 필자는 실제로이 코드가 무엇인지 잘 모르겠다. 나와 다른 것들과 함께,하지만 app에서 코드를 생성하고 FF SQLite Manager에서 실행하면 성공적으로 실행되었다. 또한 나는 또 다른 비슷한 질문을 읽었지 만 그들 모두는 안드로이드와 이오스에 관심이있다. 어떤 도움을 주셔서 감사합니다.
댓글 : 시작시
이 코드 검사 데이터베이스 : 내 문제에 대한
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
연결 문자열의 DB 위치와 SQLite Manager에서 확인중인 DB가 동일하다는 것을 확인 했습니까? – RandomEngy