数据库简介:
SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入 式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持 Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如Tcl、PHP、Java、C++、.Net等,还有ODBC接口,同样比起 Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快。
1.SQLiteOpenHelper介绍
该类是SQLiteDatabase一个辅助类。这个类主要生成一 个数据库,并对数据库的版本进行管理。当在程序当中调用这个类的方法getWritableDatabase()或者getReadableDatabase()方法的时候,如果当时没有数据,那么Android系统就会自动生成一个数据库。 SQLiteOpenHelper 是一个抽象类,我们通常需要继承它,并且实现里面的3个函数:
1.onCreate(SQLiteDatabase)
在数据库第一次生成的时候会调用这个方法,也就是说,只有在创建数据库的时候才会调用,当然也有一些其它的情况,一般我们在这个方法里边生成数据库表。
2. onUpgrade(SQLiteDatabase,int,int)
当数据库需要升级的时候,Android系统会主动的调用这个方法。一般我们在这个方法里边删除数据表,并建立新的数据表,当然是否还需要做其他的操作,完全取决于应用的需求。
3. onOpen(SQLiteDatabase):
这是当打开数据库时的回调函数,一般在程序中不使用。
xml布局:
activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.sqlite_demo.MainActivity" android:orientation="vertical"> <EditText android:id="@+id/mEdt_name" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入用户名" android:textSize="20sp"/> <EditText android:id="@+id/mEdt_pass" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入密码" android:textSize="20sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/insert" android:layout_width="0dp" android:layout_weight="1" android:text="插入" android:layout_height="wrap_content" /> <Button android:id="@+id/query" android:layout_width="0dp" android:layout_weight="1" android:text="查询" android:layout_height="wrap_content" /> <Button android:id="@+id/update" android:layout_width="0dp" android:layout_weight="1" android:text="修改" android:layout_height="wrap_content" /> <Button android:id="@+id/delete" android:layout_width="0dp" android:layout_weight="1" android:text="删除" android:layout_height="wrap_content" /> </LinearLayout> <ListView android:id="@+id/mLv" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView> </LinearLayout>
运行代码:
MySqlite
package com.example.sqlite_demo; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MySqlite extends SQLiteOpenHelper{ public final static String TABLE_MY="yangyang"; //里面的四个参数:上下文丶数据库的名字丶游标(一般为空)丶版本号 public MySqlite(Context context) { super(context, "yangyang", null, 6); } //这条语句会创建一个名为 yangyang 的表,表有一个列名为 _id,并且是主键,这列的值是会自动增长的整数(例如,当你插入一行时,SQLite 会给这列自动赋值), // 另外还有两列:title( 字符 )和 value( 浮点数 )。 SQLite 会自动为主键列创建索引。 @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { String sql="create table if not exists "+TABLE_MY+"(_id integer primary key autoincrement," +"name text not null,pass text not null)"; sqLiteDatabase.execSQL(sql); } //里面的四个参数:上下文丶数据库的名字丶游标(一般为空)丶版本号 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { //更新数据库 } }
MainActivity
package com.example.sqlite_demo; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText mEdt_name, mEdt_pass; private Button mBtn_insert, mBtn_update, mBtn_query, mBtn_delete; private SQLiteDatabase db; private ListView mLv; private List<String> list=new ArrayList<>(); private ArrayAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,list); mLv.setAdapter(adapter); MySqlite sqlite=new MySqlite(this); db=sqlite.getWritableDatabase(); } private void initView() { mEdt_name = (EditText) findViewById(R.id.mEdt_name); mEdt_pass = (EditText) findViewById(R.id.mEdt_pass); mBtn_delete = (Button) findViewById(R.id.delete); mBtn_insert = (Button) findViewById(R.id.insert); mBtn_query = (Button) findViewById(R.id.query); mBtn_update = (Button) findViewById(R.id.update); mLv= (ListView) findViewById(R.id.mLv); mBtn_delete.setOnClickListener(this); mBtn_insert.setOnClickListener(this); mBtn_query.setOnClickListener(this); mBtn_update.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.insert: insertData(mEdt_name.getText().toString(),mEdt_pass.getText().toString()); break; case R.id.query: queryData(); break; case R.id.update: updateData(mEdt_name.getText().toString(),mEdt_pass.getText().toString()); break; case R.id.delete: deleteData(mEdt_name.getText().toString()); break; } adapter.notifyDataSetChanged(); } private void insertData(String name,String pass){ ContentValues values=new ContentValues(); values.put("name",name); values.put("pass",pass); db.insert(MySqlite.TABLE_MY,null,values); } private void deleteData(String name){ db.delete(MySqlite.TABLE_MY,"name=?",new String[]{name}); } private void updateData(String name,String pass){ ContentValues values=new ContentValues(); values.put("pass",pass); db.update(MySqlite.TABLE_MY,values,"name=?",new String[]{name}); } private void queryData(){ Cursor cursor=db.query(MySqlite.TABLE_MY,null,null,null,null,null,null); if(cursor!=null){ while(cursor.moveToNext()){ int id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); String pass = cursor.getString(cursor.getColumnIndex("pass")); list.add(id+" "+name+" "+pass); } } } }