2013-01-10 4 views
0

Delphi에서 SQLite 3 테이블 중 하나를 쿼리하려고합니다. (나의 databse는 세계 다. 그리고 나는 그것에 도시라고 이름이 지어지는 테이블을 만든다). 내 코드는 다음과 같습니다Delphi-sqlite 쿼리 문제 - 테이블을 찾을 수 없습니다.

procedure TForm1.Button1Click(Sender: TObject); 
begin 
// Set the path of your database file. 
    // Replace "full_path_to_your_database_file" with the absolute path 
    // to your SQLite database file. 
    SQLConnection1.Params.Add('World.db3'); 
    try 
    // Establish the connection. 
    SQLConnection1.Connected := true; 
    Button1.Enabled := true; 
    Memo1.Text := 'Connection established!'; 
    except 
    on E: EDatabaseError do 
     ShowMessage('Exception raised with message' + E.Message); 
    end; 
end; 

procedure TForm1.ShowSelectResults(results: TDataSet); 
var 
    names: TStringList; 
    i: Integer; 
    currentField: TField; 
    currentLine: string; 
begin 
    if not results.IsEmpty then 
    begin 
    results.First; 
    names := TStringList.Create; 
    results.GetFieldNames(names); 
    while not results.Eof do 
    begin 
     currentLine := ''; 
     for i := 0 to names.Count - 1 do 
     begin 
     currentField := results.FieldByName(names[i]); 
     currentLine := currentLine + ' ' + currentField.AsString; 
     end; 
     memo1.Lines.Add(currentLine); 
     results.Next; 
    end; 
    end; 
end; 

procedure TForm1.Button2Click(Sender: TObject); 
var 
    results: TDataSet; 
    query: String; 
begin 
    Memo1.Clear; 
    // A random query 
    query := 'SELECT * FROM City;'; 

    try 
    // Execute the query on the database. 
    SQLConnection1.Execute(query, nil, results); 
    except 
    on E: Exception do 
     Memo1.Text := 'Exception raised with message: ' + E.Message; 
    end; 
    // Show the results of the query in a TMemo control. 
    ShowSelectResults(results); 
end; 

내가 컴파일하고이 코드를 실행하면,이 데이터베이스에 연결지고; 하지만이 오류가 발생합니다. "메시지로 예외 발생 : 해당 테이블 없음 : 도시" 나는 왜이 오류에 타격을가하는지 알기 위해 몇 시간과 시간을 보냈습니다. 이 코드의 여러 버전을 사용해 보았습니다. 어느 쪽이든 작동하지 않는 것 같습니다. 이와 관련하여 도움이된다면 큰 도움이됩니다.

답변

3

SQLite가 데이터베이스 파일을 찾을 수 없으면 새 파일을 만들어 열어보십시오.
항상은 데이터베이스 파일의 절대 경로를 사용해야합니다. 나는 그것을 하는지를 비교 한

SQLConnection1.Params.Add('Database=' + ExtractFilePath(ParamStr(0)) + 'World.db3'); 
+0

:

또한, 당신은 연결 매개 변수의 매개 변수 이름을 포함하는 것을 잊었다. World.db3은 같은 디렉터리에 있습니다. – jimsweb

+2

은 런타임에 응용 프로그램의 _ "현재 디렉토리"_ _ 현재 디렉토리 _입니까? – jachguate

+0

예 "같은 디렉토리"와 "현재 디렉토리"는 동일합니다. :) – jimsweb

관련 문제