2012-01-19 3 views
0

현재 이클립스에서 데이터베이스를 만들려고합니다. 사용자는 이름을 입력하고 데이터베이스에 저장할 수 있습니다. 그 외에도 저장 데이터가 다음 페이지에 표시됩니다.이클립스에서 sqlite 데이터베이스 만들기

오류가 발생하여 코드가 정확한지 확실하지 않습니다. 여기에서 도움을 얻었다 :

<?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" 
/> 

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

</LinearLayout> 

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/android:list"> 
</ListView> 

SaveData.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 SaveData extends SQLiteOpenHelper 
{ 
// Name & the version of Database. 
public static final String DATABASE_NAME = "saveData_database"; 
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_1 
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);"; 

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

public static final String TAG_1 = "PICTURES_TABLE"; 

private Context context; 

// Constructor 
public SaveData(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_1, null, initialValues); 
     } 

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

main.xml에 : 아래

database 내 코드입니다

01-19 : 10 : 29 : 24.158 : E/AndroidRuntime (488)는 자바

DataAdapter.java

package com.mp.Testing; 

import com.mp.Testing.DataAdapter; 
import com.mp.Testing.SaveData; 

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 
{ 
// Database table name 
public static final String DATABASE_TABLE_1 = "pictures"; 

// Columns present in DATABASE_TABLE_1 
public static final String PICTURES_ROWID = "_id"; 
public static final String PICTURES_FILE = "pictures_file"; 
public static final String PICTURES_COUNT = "pictures_count"; 

// Object for SQLiteDatabase 
private SQLiteDatabase database; 

// 
public static final String TAG = "COMMON_NAMES_TABLE"; 

// Creating variable 
private SaveData save_data; 

public DataAdapter() 
{ 

} 

// Open connection of database 
public DataAdapter open(Context context) throws SQLException 
{ 
    Log.i(TAG, "OPening DataBase Connection...."); 
    save_data = new SaveData(context); 
    database = save_data.getWritableDatabase(); 
    return this; 
} 

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

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

// Fetching all the pictures 
public Cursor fetchAllPictures() 
{ 
    return database.query(DATABASE_TABLE_1, new String[] {PICTURES_ROWID, PICTURES_FILE}, null, null, null, null, PICTURES_ROWID); 
} 

// Fetching the pictures 
public Cursor fetchPictures(long commonNameId) throws SQLException 
{ 
    Cursor mCursor = database.query(true, DATABASE_TABLE_1, new String[] 
    { 
     PICTURES_ROWID, PICTURES_FILE}, PICTURES_ROWID + "=" + 
     commonNameId, null, null, null, null, null); 

     if(mCursor != null) 
     { 
      mCursor.moveToFirst(); 
     } 
    return mCursor; 
} 

// Fetching pictures 
public Cursor fetch_all_pictures() 
{ 
    return database.query(DATABASE_TABLE_1, new String[] {PICTURES_ROWID, PICTURES_FILE}, null, null, null, null, null); 
} 

// Update the database 
public boolean updatePictures(int commonNameId, String commonName, String commonNameCount) 
{ 
    ContentValues args = new ContentValues(); 
    args.put(PICTURES_FILE, commonName); 
    args.put(PICTURES_COUNT, commonNameCount); 

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

Testing.java는

package com.mp.Testing; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class Testing extends ListActivity 
{ 
DataAdapter cnTable; 
ListView cnListView; 
Cursor c; 

private static final int DATA_ACTIVITY_START = 1; 
private static final int PICTURES_ACTIVITY_START = 0; 

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

    cnTable = new DataAdapter(); 
    cnTable.open(getApplicationContext()); 

    c = cnTable.fetchAllPictures(); 
    startManagingCursor(c); 

    if(c!=null) 
    { 
     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
     R.layout.solution, c, 
     new String[] {c.getColumnName(1)}, 
     new int[] {R.id.saveButton}); 
     setListAdapter(adapter); 
    } 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
    super.onListItemClick(l, v, position, id); 

    c.moveToPosition(position); 

    Intent i = new Intent(this, Testing.class); 
    i.putExtra(DataAdapter.PICTURES_ROWID, id); 
    i.putExtra(DataAdapter.PICTURES_FILE, c.getString(c.getColumnIndexOrThrow(DataAdapter.PICTURES_FILE))); 
    startActivityForResult(i, PICTURES_ACTIVITY_START); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) 
{ 
    super.onActivityResult(requestCode, resultCode, intent); 
    //Bundle extras = intent.getExtras(); 
    switch(requestCode) 
    { 
     default: break; 
    } 
} 

@Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    c.close(); 
    cnTable.close(); 
} 
} 

이 I받은 오류라고 .lang.RuntimeException : 활동을 시작할 수 없습니다. ComponentInfo {com.mp.Testing/com.mp.Testing} : android.database.sqlite.SQLiteException : "pictures"근처 : 구문 오류 : 그림

희망 모두 내가 그 문제를 해결하고 내가 작성한 모든 오류를 지적하도록 도울 수있다. 감사합니다. onCreate() 메소드의 SaveData에

+0

getApplicationContext()를 사용할 필요가 없으며 cnTable.open (this);을 사용할 수 있습니다. – Pandy

답변

0

이 있으면 database.execSQL (DATABASE_TABLE_1)이 아닙니다. 하지만 database.execSQL (CREATE_DATABASE_TABLE_1);

+0

감사합니다. 내 응용 프로그램은 원활하게 실행할 수 있지만 화면이 비어 있습니다. UI를 볼 수 없습니다. 어떤 생각이 생겼습니까? – Eric

+0

데이터베이스가 비어 있어야합니다. –

+0

사용자가 데이터베이스에 이름을 저장하는 방법과 이름이 다음 페이지에 동시에 표시되는 방법이 있습니까? – Eric

관련 문제