打开数据库读取数据
private MyDatabaseHelper dbHelper; dbHelper=new MyDatabaseHelper(this,"List.db",null,1); dbHelper.getWritableDatabase(); SQLiteDatabase db=dbHelper.getWritableDatabase(); Cursor cursor=db.query("List",null,null,null, null,null,null);//读取数据库里面专业是所有信息 if(cursor.moveToFirst()){ do{ String inorout=cursor.getString(cursor.getColumnIndex("收支"));//根据key值读取信息 String num=cursor.getString(cursor.getColumnIndex("金额")); String kind=cursor.getString(cursor.getColumnIndex("分类")); String txt=cursor.getString(cursor.getColumnIndex("备注")); String date=cursor.getString(cursor.getColumnIndex("日期")); str+=""+inorout+";"+ ""+num+";"+ ""+kind+";"+ ""+txt+";"+date+" ";//将数据库信息存到str中并换行 }while (cursor.moveToNext()); } cursor.close(); txt.setText(str);//打印信息
MyDatabaseHelper.java
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_LIST="create table List(" +"id integer primary key autoincrement," +"收支 text," +"金额 text," +"分类 text," +"备注 text," +"日期 text)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){ super(context,name,factory,version); Log.v("Log","创建数据库"); mContext=context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_LIST); Log.v("Log","创建成功"); Toast.makeText(mContext, "创建成功", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }