2012-01-22 2 views
0

私は現在、eclipseでデータベースを作成しようとしています。ユーザーは自分の名前を入力してデータベースに保存することができます。それ以外に、セーブデータは次のページに表示されます。SQLエラー:そのようなテーブルなし

私は以前あなたに教えてくれたことを試してみましたが、同様のエラーが見つかりました。以下は

更新されたコードです:

main.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Enter your name below:" 
/> 

<EditText 
android:id="@+id/nameText" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
/> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Enter your langtitude below:" 
/> 

<EditText 
android:id="@+id/langText" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
/> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Enter your longtitude below:" 
/> 

<EditText 
android:id="@+id/longText" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
/> 

<Button 
android:id="@+id/saveButton" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:text="Save" 
/> 

solution.xml

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/list"> 
</ListView> 

DataAdapter.java

package com.mp.Testing; 

import java.io.IOException; 

import com.mp.Testing.DataAdapter; 
import com.mp.Testing.DataListActivity; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.util.Log; 

public class DataAdapter 
{ 
// Name of the database 
private static final String DATABASE_NAME = "information"; 

// Names of the Tables in Database 
public static final String DATABASE_TABLE_1 = "pictures"; 
public static final String DATABASE_TABLE_2 = "data"; 

// Version of the database 
private static final int DATABASE_VERSION = 1; 

// Columns present in DATABASE_TABLE 
public static final String PICTURES_ROWID = "_id"; 
public static final String PICTURES_FILE = "pictures_file"; 
public static final String DATA_NAME = "_name"; 
public static final String DATA_LANGTITUDE = "pictures_langtitude"; 
public static final String DATA_LONGTITUDE = "pictures_longtitude"; 

// Help to create & manage the SQLiteDatabase 
private DataDBHelper DbHelper; 

// CRUD on SQLiteDatabase 
private SQLiteDatabase database; 

// SQL query string for creating DATABASE_TABLE_1 
static final String CREATE_DATABASE_TABLE_1 = 
      "create table " + DATABASE_TABLE_1 + " (" + PICTURES_ROWID + 
      " integer primary key autoincrement, " + PICTURES_FILE + 
      " text not null);"; 

// SQL query string for creating DATABASE_TABLE_2 
static final String CREATE_DATABASE_TABLE_2 = 
      "create table " + DATABASE_TABLE_2 + " (" + DATA_NAME + 
      " integer primary key autoincrement, " + DATA_LANGTITUDE + 
      " text not null, " + DATA_LONGTITUDE + " text not null);"; 

// Context object associated with the SQLite database object 
private final Context Ctx; 

// Constructor 
public DataAdapter(Context ctx) 
{ 
    this.Ctx = ctx; 
} 

// Open database connection 
public DataAdapter open() throws android.database.SQLException 
{ 
    DbHelper = new DataDBHelper(Ctx); 
    database = DbHelper.getWritableDatabase(); 
    return this; 
} 

// Close database connection 
public void close() 
{ 
    DbHelper.close(); 
} 

// Create the database_1 & define the values that is being insert 
public long createPictures(String file) 
{            
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(PICTURES_FILE, file); 

    return database.insert(DATABASE_TABLE_1, null, initialValues); 
} 

// Create the database_2 & define the values that is being insert 
public long createData(String lan, String lon) 
{            
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(DATA_LANGTITUDE, lan); 
    initialValues.put(DATA_LONGTITUDE, lon); 

    return database.insert(DATABASE_TABLE_2, null, initialValues); 
} 

// Delete the ID in the database_1 
public boolean deletePictures(long picsId) 
{        
    return database.delete(DATABASE_TABLE_1, PICTURES_ROWID + "=" + picsId, null) > 0;   
} 

// Delete the ID in the database_2 
public boolean deleteData(long dataId) 
{        
    return database.delete(DATABASE_TABLE_2, DATA_NAME + "=" + dataId, null) > 0;   
} 

// Find all the data of database_1 from the system 
public Cursor fetchAllPictures() 
{          
    return database.query(DATABASE_TABLE_1, new String[] {PICTURES_ROWID, PICTURES_FILE}, null, null, null, null, null); 
} 

// Find all the data of database_2 from the system 
public Cursor fetchAllData() 
{          
    return database.query(DATABASE_TABLE_2, new String[] {DATA_NAME, DATA_LANGTITUDE, DATA_LONGTITUDE}, null, null, null, null, null); 
} 

// Fetch Pictures according to ID 
public Cursor fetchPictures(long picsId) throws SQLException 
{    
    Cursor dCursor = 
    database.query(true, DATABASE_TABLE_1, new String[] {PICTURES_ROWID, 
    PICTURES_FILE}, PICTURES_ROWID + "=" + 
    picsId, null, null, null, null, null);  

    // Go to the first record 
    if (dCursor != null) 
    { 
     dCursor.moveToFirst();            
    } 
    return dCursor; 
} 

// Fetch Data according to ID 
public Cursor fetchData(long dataId) throws SQLException 
{    
    Cursor dCursor = 
    database.query(true, DATABASE_TABLE_2, new String[] {DATA_NAME, 
    DATA_LANGTITUDE, DATA_LONGTITUDE}, DATA_NAME + "=" + 
    dataId, null, null, null, null, null);  

    // Go to the first record 
    if (dCursor != null) 
    { 
     dCursor.moveToFirst();            
    } 
    return dCursor; 
} 

// Update the database_1 
public boolean updatePictures(long picsId, String file) 
{            
    ContentValues args = new ContentValues();        
    args.put(PICTURES_FILE, file); 

    return database.update(DATABASE_TABLE_1, args, PICTURES_ROWID + "=" + picsId, null) > 0;  
} 

// Update the database_2 
public boolean updateData(long dataId, String lan, String lon) 
{            
    ContentValues args = new ContentValues();        
    args.put(DATA_LANGTITUDE, lan); 
    args.put(DATA_LONGTITUDE, lon); 

    return database.update(DATABASE_TABLE_2, args, DATA_NAME + "=" + dataId, null) > 0;  
} 
} 

DataListActivity.java

package com.mp.Testing; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import android.app.ListActivity; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView.AdapterContextMenuInfo; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class DataListActivity extends ListActivity 
{ 
private static final int ACTIVITY_CREATE=0; 
private static final int ACTIVITY_EDIT=1; 

// Define the variables 
private DataAdapter DbHelper; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    DbHelper = new DataAdapter(this); 
    DbHelper.open(); 
    fillData();               
    registerForContextMenu(getListView()); 
} 

// Fill the data in the database 
private void fillPictures() 
{ 
    Cursor dbCursor = DbHelper.fetchAllData();    
    startManagingCursor(dbCursor);         

    // Creating an array to specify the fields we want 
    String[] dat = new String[]{DataAdapter.PICTURES_FILE}; 

    // An array of the fields we want to bind in the view 
    int[] dato = new int[]{R.id.nameText};          

    // Create a simple cursor adapter & display it 
    SimpleCursorAdapter reminders = new SimpleCursorAdapter(this, R.layout.solution, dbCursor, dat, dato);       
    setListAdapter(reminders);            
} 

// Fill the data in the database 
private void fillData() 
{ 
    Cursor dbCursor = DbHelper.fetchAllData();    
    startManagingCursor(dbCursor);         

    // Creating an array to specify the fields we want 
    String[] lan = new String[]{DataAdapter.DATA_LANGTITUDE}; 
    String[] lon = new String[]{DataAdapter.DATA_LONGTITUDE}; 

    // An array of the fields we want to bind in the view 
    int[] lanto = new int[]{R.id.langText}; 
    int[] lonto = new int[]{R.id.longText}; 

    // Create a simple cursor adapter & display it 
    SimpleCursorAdapter landers = new SimpleCursorAdapter(this, R.layout.solution, dbCursor, lan, lanto);       
    setListAdapter(landers); 

    // Create a simple cursor adapter & display it 
    SimpleCursorAdapter londers = new SimpleCursorAdapter(this, R.layout.solution, dbCursor, lon, lonto);       
    setListAdapter(londers); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
    super.onListItemClick(l, v, position, id); 
    Intent i = new Intent(this, DataEditActivity.class); 
    i.putExtra(DataAdapter.PICTURES_ROWID, id); 
    i.putExtra(DataAdapter.DATA_NAME, id); 
    startActivityForResult(i, ACTIVITY_EDIT); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
    super.onActivityResult(requestCode, resultCode, intent); 
    fillData();               
} 

@Override 
public boolean onContextItemSelected(MenuItem item) 
{      
    switch(item.getItemId()) 

    { 
     case R.id.list: 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();  
     DbHelper.deleteData(info.id);        
     fillData();             
     return true; 
    } 
    return super.onContextItemSelected(item); 
} 
} 

DataEditActivity.java

package com.mp.Testing; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.Toast; 

public class DataEditActivity extends Activity implements OnClickListener 
{ 
private DataAdapter DbHelper; 
private Long mPicsId; 
private Long mDataId; 
private EditText ET; 
private EditText LAT; 
private EditText LOT; 
private Button SB; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    DbHelper = new DataAdapter(this); 

    ET = (EditText) findViewById(R.id.nameText); 
    LAT = (EditText) findViewById(R.id.langText); 
    LOT = (EditText) findViewById(R.id.longText); 
    SB = (Button) findViewById(R.id.saveButton); 

    mPicsId = savedInstanceState != null         
    ? savedInstanceState.getLong(DataAdapter.PICTURES_ROWID): null; 
    mDataId = savedInstanceState != null         
    ? savedInstanceState.getLong(DataAdapter.DATA_NAME): null; 
    registerButtonListenersAndSetDefaultText(); 
} 

private void registerButtonListenersAndSetDefaultText() 
{ 
    // TODO Auto-generated method stub 
    SB.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View view) 
     { 
     saveState();             
      setResult(RESULT_OK);          
      Toast.makeText(DataEditActivity.this,     
      getString(R.string.message), 
      Toast.LENGTH_SHORT).show(); 
      finish();             
     } 
    }); 
} 

// Intent to start the activity 
private void setRowIdFromIntent() 
{          
    if (mPicsId == null) 
    { 
     Bundle extras = getIntent().getExtras(); 
     mPicsId = extras != null 
     ? extras.getLong(DataAdapter.PICTURES_ROWID): null; 
    } 
    if (mDataId == null) 
    { 
     Bundle extras = getIntent().getExtras(); 
     mDataId = extras != null 
     ? extras.getLong(DataAdapter.DATA_NAME): null; 
    } 
} 

// Database is close when it is pause 
@Override 
protected void onPause() 
{ 
    super.onPause(); 
    DbHelper.close();              
} 

// Resume the database 
@Override 
protected void onResume() 
{             
    super.onResume(); 
    DbHelper.open();              
    setRowIdFromIntent();             
    populateFields();              
} 

// Populate the form 
private void populateFields() 
{           
    if (mPicsId != null) 
    { 
     Cursor pics = DbHelper.fetchData(mPicsId);     
     startManagingCursor(pics);    

     ET.setText(pics.getString(
     pics.getColumnIndexOrThrow(DataAdapter.PICTURES_FILE)));  
    } 
    if (mDataId != null) 
    { 
     Cursor data = DbHelper.fetchData(mDataId);     
     startManagingCursor(data);    

     LAT.setText(data.getString(
     data.getColumnIndexOrThrow(DataAdapter.DATA_LANGTITUDE))); 
     LOT.setText(data.getString(
     data.getColumnIndexOrThrow(DataAdapter.DATA_LONGTITUDE))); 
    } 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) 
{ 
    super.onSaveInstanceState(outState); 
    outState.putLong(DataAdapter.PICTURES_ROWID, mPicsId); 
    outState.putLong(DataAdapter.DATA_NAME, mDataId); 
} 

private void saveState() 
{ 
    String file = ET.getText().toString(); 
    String lan = LAT.getText().toString(); 
    String lon = LOT.getText().toString(); 

    if (mPicsId == null && mDataId == null) 
    {             
     long id = DbHelper.createPictures(file); 
     long ild = DbHelper.createData(lan, lon); 

     if (id > 0 && ild > 0) 
     {              
      mPicsId = id; 
      mDataId = ild; 
     } 
    } 
    else 
    { 
     DbHelper.updatePictures(mPicsId, file); 
     DbHelper.updateData(mDataId, lan, lon); 
    } 
} 

@Override 
public void onClick(View v) 
{ 
    Intent i = new Intent(DataEditActivity.this, DataEditActivity.class); 
    startActivity(i); 
} 
} 

DataDBHelper.java

package com.mp.Testing; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DataDBHelper extends SQLiteOpenHelper 
{ 
// Name & the version of Database. 
public static final String DATABASE_NAME = "information"; 
public static final int DATABASE_VERSION = 1; 

// Names of the Tables in Database 
public static final String DATABASE_TABLE_1 = "pictures"; 
public static final String DATABASE_TABLE_2 = "data"; 

// Columns present in DATABASE_TABLE 
public static final String PICTURES_ROWID = "_id"; 
public static final String PICTURES_FILE = "pictures_file"; 
public static final String DATA_NAME = "_name"; 
public static final String DATA_LANGTITUDE = "pictures_langtitude"; 
public static final String DATA_LONGTITUDE = "pictures_longtitude"; 

// SQL query string for creating DATABASE_TABLE_1 
static final String CREATE_DATABASE_TABLE_1 = 
      "create table " + DATABASE_TABLE_1 + " (" + PICTURES_ROWID + 
      " integer primary key autoincrement, " + PICTURES_FILE + 
      " text not null);"; 

// SQL query string for creating DATABASE_TABLE_2 
static final String CREATE_DATABASE_TABLE_2 = 
      "create table " + DATABASE_TABLE_2 + " (" + DATA_NAME + 
      " integer primary key autoincrement, " + DATA_LANGTITUDE + 
      " text not null, " + DATA_LONGTITUDE + " text not null);"; 

// To execute the SQL command 
@Override 
public void onCreate(SQLiteDatabase database) 
{ 
    database.execSQL(CREATE_DATABASE_TABLE_1); 
    database.execSQL(CREATE_DATABASE_TABLE_2); 
    Log.d("SaveData", "Created DB"); 
} 

public static final String TAG_1 = "PICTURES_TABLE"; 
public static final String TAG_2 = "DATA_TABLE"; 

private Context context; 

// Constructor 
public DataDBHelper(Context context) 
{ 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    this.context = context; 
} 

// Upgrading the database version 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{ 
    // TODO Auto-generated method stub 
} 

// Inserting pictures into database 
private void insertDataIntoPictures(SQLiteDatabase db) 
{ 
    try 
    { 
     InputStream is = context.getResources().openRawResource(R.raw.picture); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     String strLine = null; 

     while ((strLine = (br.readLine()).trim()) != null) 
     { 
      String[] temp = null; 

      ContentValues initialValues = new ContentValues(); 

      initialValues.put(PICTURES_FILE, temp[0].trim()); 

      db.insert(DATABASE_TABLE_1, null, initialValues); 
     } 
    is.close(); 
    } 
    catch (Exception e) 
    { 
     Log.d(TAG_1, "Error while inserting common names into table"); 
    } 
} 

// Inserting data into database 
private void insertDataIntoData(SQLiteDatabase db) 
{ 
    try 
    { 
     InputStream is = context.getResources().openRawResource(R.raw.data); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     String strLine = null; 

     while ((strLine = (br.readLine()).trim()) != null) 
     { 
      String[] temp = null; 

      ContentValues initialValues = new ContentValues(); 

      initialValues.put(DATA_LANGTITUDE, temp[0]); 
      initialValues.put(DATA_LONGTITUDE, temp[1]); 

      db.insert(DATABASE_TABLE_2, null, initialValues); 
     } 
    is.close(); 
    } 
    catch (Exception e) 
    { 
     Log.d(TAG_2, "Error while inserting common names into table"); 
    } 
} 
} 

これは私が受信したエラーである:

1月23日18: 20:39.465:E/Database(4837):android.database.sqlite.SQLiteE xception:そのようなテーブルはありません:data:、コンパイル中:INSERT INTOデータ(pictures_longtitude、pictures_langtitude)VALUES(?,?);

私はすべてそれを解決し、自分のコードで書いたエラーを指摘してくれることを願っています。 ありがとうございました!

答えて

0

CREATE_DATABASE_TABLE_1という文字列があり、onCreateにexecするが、CREATE_DATABASE_TABLE_2という文字列はなく、データベース上で実行されていない。 DATABASE_TABLE_2"data"に設定されていますので、CREATE TABLEスクリプトを作成し、DataDBHelperonCreateに使用するだけです。

+0

こんにちは、私を助けてくれてありがとう!私はあなたが私に言ったことをして、エラーに遭遇する。上のコードは更新されたバージョンです。 – Eric

+0

ログに「Created DB」が表示されていますか? onCreateが起動しているかどうかを判断する必要があります。 – oliverseal

-2

読みにくいコードが多すぎます。

正常に接続しているデータベースに接続していないか、正常に接続しているデータベースにテーブルがまだ作成されていない可能性があります。両方を確認してください。

パーシスタンスコードを1つのクラスに分離してテストすることができます。そうでない場合は、これを可能にするように再設計することを検討します。

+0

本当にあまり役に立ちません。あなたがコードを読むのに気を使うことができないなら、答えを推測するだけではいけません! – barry

+0

他の答えはあなたがテーブルを作成しなかったことを指摘しています。私は速く、あなたのコードを読んで気にせずにそれを考え出した。いくつかの間違いは何かを理解するために何の努力もしません。あなたの前提をチェックしてください.... – duffymo

+0

冷やせる!私はあなたが私を助けてくれたことに感謝しています。 :)私はあなたが私に言ったことをして、エラーに遭遇する。上のコードは更新されたバージョンです。 – Eric

0

DataDBHelper.javaのonCreate()メソッドでは、2番目のテーブルを作成していません。ここ

@Override 
public void onCreate(SQLiteDatabase database) 
{ 
    database.execSQL(CREATE_DATABASE_TABLE_1); 
    database.execSQL(CREATE_DATABASE_TABLE_2); 
    Log.d("SaveData", "Created DB"); 
} 

あなたがテーブルを作成する必要が

@Override 
public void onCreate(SQLiteDatabase database) 
{ 
    database.execSQL(CREATE_DATABASE_TABLE_1); 
    Log.d("SaveData", "Created DB"); 
} 

CREATE_DATABASE_TABLE_2 = "表2のための私のSQL stament"。

+0

こんにちは、私を助けてくれてありがとう!私はあなたが私に言ったことをして、エラーに遭遇する。上のコードは更新されたバージョンです。 – Eric

0

あなたのonCreateだけのために写真

@Override 
public void onCreate(SQLiteDatabase database) {  database.execSQL(CREATE_DATABASE_TABLE_1);  
Log.d("SaveData", "Created DB"); } 

しかし、コンパイル時に、あなたの例外メッセージごとにあなたがデータ

に挿入しようとしているため、テーブルを作成していますデータ。INSERT INTO( pictures_file、pictures_count)VALUES(?,?);

データテーブルに挿入する場合はデータテーブルを追加し、それ以外の場合はテーブル名が間違って渡される場所をチェックしてください。

+0

こんにちは、私を助けてくれてありがとう!私はあなたが私に言ったことをして、エラーに遭遇する。上のコードは更新されたバージョンです。 – Eric

関連する問題