2012-12-18 4 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; } 
} 

나는 돈 여기에 내가 테이블을 만든 방법입니다 필자는 실제로이 코드가 무엇인지 잘 모르겠다. 나와 다른 것들과 함께,하지만 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 
      } 

그리고 최종 업데이트합니다. 알다시피 데이터베이스에 다른 테이블이 있으므로 사진 예제를 추가하기 전에 모든 기능이 제대로 작동합니다. 오류가 발생하면 데이터베이스 작업과 관련된 작업을 수행 할 수 없습니다. 모든 조작은 동일한 오류를 리턴하지만 해당 테이블에 대해 리턴합니다.

+1

매개 변수화 된 쿼리를 살펴 보시기 바랍니다 : http://stackoverflow.com/questions/4141599/parameterized-queries-in-sqlite. SQL 인젝션에 대해 들어 본 적이 있습니까? – iamkrillin

+0

CREATE TABLE 쿼리를 어떻게 실행 했습니까? – PinnyM

+0

연결 문자열의 DB 위치와 SQLite Manager에서 확인중인 DB가 동일하다는 것을 확인 했습니까? – RandomEngy

답변

1

답변을 얻었습니다. 연결할 데이터베이스 유형을 선택할 수있는 연결 옵션 양식이 있습니다. 매개 변수 중 하나가 경로입니다. 다음 방법으로 저장했습니다 :

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

데이터베이스 파일이 앱 폴더 내에있을 때 경로가 더 깨끗해 보입니다. 양식에 파일 대화 상자가 있는데 여기에서 사진 예제를 만들 수 있습니다. 당신이 이미 알고있는 다음 이야기. 앱이 사진을 열어 폴더에서 데이터베이스 파일을 검색하기 시작했습니다. 상대 경로에주의하십시오.

0

LoadForm.ConnString이 올바른 계정을 사용합니까? Sqlite 관리자에서 한 가지 방법으로 로그인하여 하나의 스키마에 테이블을 만들었지 만 응용 프로그램이 다른 계정/스키마로 로그인하면 해당 테이블은 현재 스키마의 응용 프로그램에서 찾을 수 없습니다.

+0

제 질문의 맨 아래에있는 제 코멘트를보십시오. 내 응용 프로그램은 스키마에있는 모든 테이블이 손상된 db 파일 등으로 작업하지 못하는지 확인합니다. –

관련 문제