zoukankan      html  css  js  c++  java
  • android之数据库SQLite(一)

    创建数据库

    首先定义SQLiteOpenHelper的子类

    代码如下:

    package com.example.myandroid;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class MyDatabaseHelper extends SQLiteOpenHelper{
    	private static final String DATABASENAME = "mldn.db";
    	private static final int DATABASEVERSION = 1;
    	private static final String TABLENAME = "mytab";
    	public MyDatabaseHelper(Context context){
    		super(context,DATABASENAME,null,DATABASEVERSION);
    	}
    	public MyDatabaseHelper(MainActivity context) {
    		super(context,DATABASENAME,null,DATABASEVERSION);
    	}
    	public void onCreate(SQLiteDatabase db){
    		String sql = "CREATE TABLE "+TABLENAME+"("+
    		"id			INTEGER			PRIMARY KEY,"+
    		"name		VERCHAR(50)		NOT NULL,"+
    		"birthday	DATE			NOT NULL)";
    		db.execSQL(sql);
    	}
    	public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
    		String sql = "DROP TABLE IF EXITS "+TABLENAME;
    		db.execSQL(sql);
    		this.onCreate(db);
    	}
    
    }
    

      然后定义Activity类

    代码如下:

    package com.example.myandroid;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.util.Scanner;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.view.ContextMenu;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ExpandableListAdapter;
    import android.widget.ExpandableListView;
    import android.widget.ExpandableListView.OnChildClickListener;
    import android.widget.ExpandableListView.OnGroupClickListener;
    import android.widget.ExpandableListView.OnGroupCollapseListener;
    import android.widget.ExpandableListView.OnGroupExpandListener;
    import android.widget.TabHost;
    import android.widget.TabHost.TabSpec;
    import android.widget.TextView;
    import android.widget.Toast;
    
    
    public class MainActivity extends Activity {
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            SQLiteOpenHelper helper =  new MyDatabaseHelper(this);
            helper.getWritableDatabase();
        }
       
    }
    

      运行后创建数据库,可以在DDMS中的datadata包名databases中看到相应的数据库

    在启动虚拟机的情况下,可以通过命令行的方式进入数据库。

    具体如下:

    1.在命令行方式下输入adb shell,进入shell命令行方式,如图:

     

    注:前两次是由于没有启动起来虚拟机

    2.通过cd命令进入mldn.db所在的路径:datadata包名databases

    3.通过ls命令,查找路径下的内容,可以发现相应的数据库mldn.db。

    4.输入sqlite3 mldn.db命令,进入sqlite数据库

    5.输入.schema命令,查询数据库当中的数据表。

    此时采用sql语句可以进行对表的操作。

    态度决定高度,细节决定成败,
  • 相关阅读:
    大话设计模式笔记 装饰模式
    大话设计模式笔记 依赖倒转原则
    大话设计模式笔记 单一职责原则 开放-封闭原则
    Effective Java 英文 第二版 读书笔记 Item 5:Avoid creating unnecessary objects.
    Effective Java 英文 第二版 读书笔记 Item 4:Attempting to enforce noninstantiability by making a class abstract does not work.
    Effective Java 英文 第二版 读书笔记 Item 3:Enforce the singleton property with a private constructor or an enum type.
    Effective Java 英文 第二版 读书笔记 Item 2:Consider a builder when faced with many constructor parameters.
    大话设计模式笔记 策略模式
    大话设计模式笔记 简单工厂模式
    jvm的垃圾回收算法
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/3987890.html
Copyright © 2011-2022 走看看