更好的解决方案是将数据库保存在文件夹中:/data/data/you.package.name/database,使用 SQLiteDatabaseOpenHelper。阅读这里。我希望我有所帮助
编辑 :
这是简单的代码:
package com.radzik.mypersolnalnotepad;
import static com.radzik.mypersolnalnotepad.Stale.HASH;
import static com.radzik.mypersolnalnotepad.Stale.INT1;
import static com.radzik.mypersolnalnotepad.Stale.INT2;
import static com.radzik.mypersolnalnotepad.Stale.INT3;
import static com.radzik.mypersolnalnotepad.Stale.KIEDY;
import static com.radzik.mypersolnalnotepad.Stale.KTO;
import static com.radzik.mypersolnalnotepad.Stale.KTORY;
import static com.radzik.mypersolnalnotepad.Stale.LOGIN;
import static com.radzik.mypersolnalnotepad.Stale.NAZWA_TABELI1;
import static com.radzik.mypersolnalnotepad.Stale.NAZWA_TABELI2;
import static com.radzik.mypersolnalnotepad.Stale.NAZWA_TABELI3;
import static com.radzik.mypersolnalnotepad.Stale.TRESC;
import static com.radzik.mypersolnalnotepad.Stale.TYTUL;
import static com.radzik.mypersolnalnotepad.Stale.WANN;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Bazodanowiec extends SQLiteOpenHelper {
public static final String NAZWA_BAZY = "database.db";
public static final int WERSJA_BAZY = 1;
public Bazodanowiec(Context kon)
{
super(kon, NAZWA_BAZY, null, WERSJA_BAZY );
}
public void onCreate(SQLiteDatabase bd)
{
bd.execSQL("CREATE TABLE IF NOT EXISTS " + NAZWA_TABELI1 + " (" + INT1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + LOGIN + " VARCHAR(60) NOT NULL, " + HASH + " VARCHAR(64) NOT NULL);");
bd.execSQL("CREATE TABLE IF NOT EXISTS " + NAZWA_TABELI2 + " (" + INT2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TYTUL + " VARCHAR(60) NOT NULL, " + TRESC + " TEXT NOT NULL, " + KIEDY + " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " + KTO + " INTEGER NOT NULL, FOREIGN KEY(kto) REFERENCES konta(id1));");
bd.execSQL("CREATE TABLE IF NOT EXISTS " + NAZWA_TABELI1 + " (" + INT3 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KTORY + " INTEGER NOT NULL, " + WANN + " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY(ktory) REFERENCES konta(id1));");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + NAZWA_TABELI1 + ";");
db.execSQL("DROP TABLE IF EXISTS " + NAZWA_TABELI2 + ";");
db.execSQL("DROP TABLE IF EXISTS " + NAZWA_TABELI3 + ";");
onCreate(db);
}
}
“陈旧”是界面。