2016-10-01 5 views
-2

私のコードは、例外を発生させません。例外は、「そのような表:TABLE_NAME」私はそれを解決しようと一日を過ごしたが、これまでのところ成功していないTABLE_NAME</p> </blockquote> <p>:</p> <blockquote> <p>はありません、このような表

SQLite3.exeを使用してデータベースを作成しました。テーブルpersonを作成し、データを挿入します。私がテーブルpersonの内容を照会すると、私には肯定的な結果が示されます。

残念ながら、私は例外が発生したテーブルの内容を表示するには、このコードを使用する場合。

これはコードです:

procedure TForm1.connectButtonClick(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('Database=D:\testdb.db'); 
    try 
    // Establish the connection. 
    SQLConnection1.Connected := true; 
    executeButton.Enabled := true; 
    outputMemo.Text := 'Connection established!'; 
    except 
    on E: EDatabaseError do 
     ShowMessage('Exception raised with message' + E.Message); 
    end; 
end; 

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

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

procedure TForm3.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; 
     outputMemo.Lines.Add(currentLine); 
     results.Next; 
    end; 
    end; 
end; 
+0

[メッセージで上昇SQLiteの例外:そのようなテーブル]の可能な重複(http://stackoverflow.com/questions/16585953/sqlite-exception -raised - メッセージ-NO-なテーブル付き) –

+0

'ShowSelectResults(結果);' 'TForm1'のメンバーであるが、私はForm1.ShowSelectResults()'と思うTForm3.ShowSelectResults'あなたは '私たちを見る'別のコードウィッヒを持って、我々見ることができません !! –

+0

@MartynA:メモリリーク: 'results'の代わりに' names:= TStringList.Create; 'を意味すると思います!! –

答えて

1

データベースおよび/またはテーブルのいずれかが壊れているか、ミスが実行されていますが、私たちに表示されていないコードの一部であります。新しいテーブルを作成し、移入

次のコードは、デルファイシアトルで完全に実行され、outputMemoで予想される結果、即ち、1つのデータ列を生成します。

& Delphiを再起動し、DBと同じSqlite3.Dllを使用している可能性のある他のアプリケーションを閉じてから、試してみることをお勧めします。

(IはForm1にoutputMemoShowSelectResultsを移動した)

procedure TForm1.executeButtonClick(Sender: TObject); 
var 
    results: TDataSet; 
    query: String; 
begin 
    outputMemo.Clear; 

    query := 'CREATE TABLE TESTTABLE (ID BIGINT, NAME NVARCHAR(80))'; 
    SQLConnection1.Execute(query, nil, Nil); 

    query := 'INSERT INTO TESTTABLE(ID, NAME) VALUES(1, ''One'')'; 
    SQLConnection1.Execute(query, nil, Nil); 

    query := 'SELECT * FROM TESTTABLE'; 

    try 
    // Execute the query on the database. 
    SQLConnection1.Execute(query, nil, results); 

    except 
    on E: Exception do 
     outputMemo.Text := 'Exception raised with message: ' + E.Message; 
    end; 
    // Show the results of the query in a TMemo control. 
    ShowSelectResults(results); 

    query := 'DROP TABLE TESTTABLE'; 
    SQLConnection1.Execute(query, nil, Nil); 

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; 
    try 
     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; 
     outputMemo.Lines.Add(currentLine); 
     results.Next; 
     end; 
    finally 
     names.Free; 
    end; 
    end; 
end; 
+0

'names.Free'はどうですか? –

+0

@ moskito-x:良い点、ありがとう!一定。 – MartynA

+0

@MartynA実行ボタンを押したときにこの例外が発生しました 'メッセージで例外が発生しました:外​​部例外C06D007F' –

関連する問題

 関連する問題