需要使用数据库,但不知道如何在Android中实现

需要使用数据库,但不知道如何在Android中实现,android,sqlite,Android,Sqlite,需要使用现有的数据库Sqlite(我在Assets文件夹中添加),但不知道如何处理,而且我没有看到任何完整的示例可以帮助我 如果有人知道任何好的例子或能给予帮助,我将非常感激 附言:我是Android新手 谢谢创建一个活动,比如SQLDemo.java package org.example.sqldemo; import static android.provider.BaseColumns._ID; import android.app.Activity; import androi

需要使用现有的数据库Sqlite(我在Assets文件夹中添加),但不知道如何处理,而且我没有看到任何完整的示例可以帮助我

如果有人知道任何好的例子或能给予帮助,我将非常感激

附言:我是Android新手


谢谢

创建一个活动,比如SQLDemo.java

   package org.example.sqldemo;

import static android.provider.BaseColumns._ID;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class SQLDemo extends Activity {
  EventDataSQLHelper eventsData;
  TextView output;

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

    output = (TextView) findViewById(R.id.output);

    eventsData = new EventDataSQLHelper(this);
    addEvent("Hello Android Event");
    Cursor cursor = getEvents();
    showEvents(cursor);
  }

  @Override
  public void onDestroy() {
    eventsData.close();
  }

  private void addEvent(String title) {
    SQLiteDatabase db = eventsData.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(EventDataSQLHelper.TIME, System.currentTimeMillis());
    values.put(EventDataSQLHelper.TITLE, title);
    db.insert(EventDataSQLHelper.TABLE, null, values);

  }

  private Cursor getEvents() {
    SQLiteDatabase db = eventsData.getReadableDatabase();
    Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
        null, null);

    startManagingCursor(cursor);
    return cursor;
  }

  private void showEvents(Cursor cursor) {
    StringBuilder ret = new StringBuilder("Saved Events:\n\n");
    while (cursor.moveToNext()) {
      long id = cursor.getLong(0);
      long time = cursor.getLong(1);
      String title = cursor.getString(2);
      ret.append(id + ": " + time + ": " + title + "\n");
    }
    output.setText(ret);
  }
}
现在创建名为EventDataSQLHelper.java的database helper.java类

   package org.example.sqldemo;

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

/** Helper to the database, manages versions and creation */
public class EventDataSQLHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "events.db";
    private static final int DATABASE_VERSION = 1;

    // Table name
    public static final String TABLE = "events";

    // Columns
    public static final String TIME = "time";
    public static final String TITLE = "title";

    public EventDataSQLHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);

// if you keep the DATABASE_NAME as null, application will treat the DB as in memory DB which means application destroys the DB on exit
        }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TABLE + "( " + BaseColumns._ID
                + " integer primary key autoincrement, " + TIME + " integer, "
                + TITLE + " text not null);";
        Log.d("EventsData", "onCreate: " + sql);
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion >= newVersion)
            return;

        String sql = null;
        if (oldVersion == 1) 
            sql = "alter table " + TABLE + " add note text;";
        if (oldVersion == 2)
            sql = "";

        Log.d("EventsData", "onUpgrade  : " + sql);
        if (sql != null)
            db.execSQL(sql);
    }

}

我希望这对您有用:)

尝试以下代码将现有数据库从资产文件夹复制到Android inmemory中

private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

这里有一个很好的例子:

要记住的最大一点是本教程的第1部分,您必须使用一些Android元数据稍微修改数据库,以便它与框架SQLite类顺利集成


希望有帮助

谢谢,但我需要的是一个如何管理我添加到文件夹资产中的现有数据库的示例