• Android打开数据库读取数据


    打开数据库读取数据

    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) {
    
        }
    }


  • 相关阅读:
    Oracle数据库相关问题
    常用Oracle数据库查询SQL
    VS2019添加引用错误:COM组件调用返回错误HRESULT E_FAIL
    C#.NET重点知识点汇总(三)
    C#.NET重点知识点汇总(二)
    C#.NET重点知识点汇总(一)
    ajax的19道经典面试题
    C#设计模式——抽象工厂模式
    C#设计模式——工厂方法模式
    C#设计模式——简单工厂模式
  • 原文地址:https://www.cnblogs.com/linmob/p/14408425.html
Copyright © 2020-2023  润新知