我这里简单的介绍一种很简单的数据库的存储和查询的功能。
先给出数据库建立的代码:
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast;
public class MyDatabaseHelper extends SQLiteOpenHelper{
这里是建表的关键语句,创建了一个表,表名叫Book; public static final String CREATE_BOOK = "create table Book (" + "id integer primary key autoincrement, " + "weight text, " + "height text, " + "name text)"; private Context mContext;
这里的Book.db是数据库的名称,我们的表就建立在这个数据库里面,这里没有数据库的位置显示,是默认位置的,正是因为这样,这篇文章才比较简单,
不过后面我回有更详细的文章来讲述这个问题的。
private static final String name = "BookStore.db"; public MyDatabaseHelper(Context context,SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); mContext = context; }
这里是执行语句 @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_BOOK); Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
然后在主函数里面添加信息
import android.content.ContentValues; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private MyDatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new MyDatabaseHelper(this, null, 2); Button get = (Button) findViewById(R.id.querys); Button next = (Button) findViewById(R.id.next); final EditText name = (EditText) findViewById(R.id.name); final EditText weight = (EditText) findViewById(R.id.weight); final EditText height = (EditText) findViewById(R.id.height); get.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); if (name.length()!=0 && weight.length()!=0 && height.length()!=0) { ContentValues values = new ContentValues(); values.put("name", name.getText().toString()); values.put("weight", weight.getText().toString()); values.put("height", height.getText().toString()); db.insert("Book", null, values); values.clear(); Toast.makeText(MainActivity.this, "输入信息成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "信息残缺,不予许输入!", Toast.LENGTH_SHORT).show(); } } }); next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Next.class); startActivity(intent); } }); } }
然后数据的查找,删除,更改都在另外一个界面,这样是更加直观的感受到这两个不同Activity操纵到的是同一个数据库
import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.EditText; import android.widget.Toast; /** * Created by Mr-x on 2016/11/16. */ public class Next extends Activity{ MyDatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); final TextView name1 = (TextView)findViewById(R.id.name1); final TextView weight1 = (TextView)findViewById(R.id.weight1); final TextView height1 = (TextView)findViewById(R.id.height1); final EditText sear = (EditText)findViewById(R.id.sear); final EditText what = (EditText)findViewById(R.id.what); final EditText become = (EditText)findViewById(R.id.become); final EditText name2 = (EditText)findViewById(R.id.name2); final EditText name3 = (EditText)findViewById(R.id.name3); dbHelper = new MyDatabaseHelper(this, null, 2); Button querys1 = (Button)findViewById(R.id.query1); Button sure = (Button)findViewById(R.id.sure); Button change = (Button)findViewById(R.id.change); querys1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); Cursor cursor = db.query("Book", null, "name = ?", new String[]{sear.getText().toString()}, null, null, null); int i = 1;这里有一步我提醒一下,就是这里的cursor.moveToFirst()是必须写的,什么意思呢,就是将数据库文件的指向指到第一个数据集,如果没有这一行,文件就会报错
,所以必须写。 if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex("name")); int pages = cursor.getInt(cursor.getColumnIndex("weight")); int price = cursor.getInt(cursor.getColumnIndex("height")); name1.setText(name); weight1.setText(" " + pages); height1.setText("" + price); } while (cursor.moveToNext()); } cursor.close(); if (name1.length()==0) { Toast.makeText(Next.this, "请输入用户名", Toast.LENGTH_SHORT).show(); } } }); change.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (name2.length()!=0 && what.length()!=0 && become.length()!=0) { ContentValues values = new ContentValues(); values.put(what.getText().toString(), become.getText().toString()); SQLiteDatabase db = dbHelper.getWritableDatabase(); db.update("Book", values, "name = ?", new String[]{name2.getText().toString()}); Toast.makeText(Next.this, "更改成功!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Next.this, "更改数据项残缺,请重新查看", Toast.LENGTH_SHORT).show(); } } }); sure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (name3.length()!=0) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.delete("Book", "name = ?", new String[]{name3.getText().toString()}); Toast.makeText(Next.this, "删除成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Next.this, "查无此人", Toast.LENGTH_SHORT).show(); } } }); } }最后我再给出两个Activity的界面
activity—main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" android:textSize="20dp" /> <EditText android:id="@+id/name" android:layout_width="100dp" android:layout_height="wrap_content" android:background="#ffffff" android:textColor="#000000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="体重" android:textSize="20dp" android:digits="1234567890." /> <EditText android:id="@+id/weight" android:layout_width="100dp" android:layout_height="wrap_content" android:background="#ffffff" android:textColor="#000000" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="身高" android:textSize="20dp" android:digits="1234567890." /> <EditText android:id="@+id/height" android:layout_width="100dp" android:layout_height="wrap_content" android:background="#ffffff" android:textColor="#000000" /> </LinearLayout> <Button android:id="@+id/querys" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入信息" /> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一个界面" /> </LinearLayout>second这里我给出另外一篇写的特别好的数据库的文章,里面用ListView展示了从数据库里面读取并显示的情况,网址给出:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="输入你要查询的姓名"/> <EditText android:id="@+id/sear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp"/> </LinearLayout> <Button android:id="@+id/query1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="20dp" android:textColor="#000000" android:text="查询" /> <TextView android:id="@+id/name1" android:layout_width="100dp" android:textSize="20dp" android:layout_height="wrap_content" android:background="#ffffff" android:textColor="#000000" /> <TextView android:id="@+id/weight1" android:layout_width="100dp" android:textSize="20dp" android:layout_height="wrap_content" android:background="#ffffff" android:textColor="#000000" /> <TextView android:id="@+id/height1" android:layout_width="100dp" android:textSize="20dp" android:layout_height="wrap_content" android:background="#ffffff" android:textColor="#000000" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更改数据" android:textColor="#000000" android:textSize="20dp"/> <EditText android:id="@+id/name2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="姓名" android:textSize="20dp"/> <EditText android:id="@+id/what" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="选项:weight,height"/> <EditText android:id="@+id/become" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="更改为:" android:digits="1234567890."/> </LinearLayout> <Button android:id="@+id/change" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="确认更改" android:textColor="#000000" android:textSize="20dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#000000" android:text="输入需要删除的人名"/> <EditText android:id="@+id/name3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="姓名" android:textSize="20dp"/> </LinearLayout> <Button android:id="@+id/sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="20dp" android:text="确认"/> </LinearLayout>最后给出数据库支持的数据的类型:只支持null,integer,real(浮点型),text(文本),blob(二进制数据) 五种数据类型;
我再提一个东西,就是数据库的回滚问题,什么意思呢?如果有甲乙两人,一个还钱,一个收钱,那么在数据库里面操作就必须要同时成功,否则就会
出现一个钱多了,一个钱没变或者一个钱少,一个人没变的不正常结果,那么我们为了保证一致性,就提出了回滚,具体用法入下:
使用SQLiteDatabase的beginTransaction()方法可以开启一个事务,程序执行到endTransaction() 方法时会检查事务的标志是否为成功,
如果为成功则提交事务,否则回滚事务。当应用需要提交事务,必须在程序执行到endTransaction()方法之前使用
setTransactionSuccessful() 方法设置事务的标志为成功,如果不调用setTransactionSuccessful() 方法,默认会回滚事务。
注意,一定要调用db.setTransactionSuccessful().还一定不要忘记在finally{}语句块里结束事务。
http://blog.csdn.net/shulianghan/article/details/19028665