MyDatabaseHelper:
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK="create table ls("
+"id integer primary key autoincrement,"
+"age int)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, 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_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
}
}
MainActivity:
package com.example.sky.you;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new MyDatabaseHelper(this,"ls.db",null,1);
Button createDatabase=(Button)findViewById(R.id.create_database);
Button addData=(Button)findViewById(R.id.add_data);
Button queryButton=(Button)findViewById(R.id.query_data);
queryButton.setOnClickListener(this);
createDatabase.setOnClickListener(this);
addData.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()) {
case R.id.create_database:
dbHelper.getReadableDatabase();
break;
case R.id.add_data:
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("age", 12);
db.insert("ls", null, values);
values.clear();
values.put("age", 13);
db.insert("ls", null, values);
values.clear();
values.put("age",111);
db.insert("ls",null,values);
Toast.makeText(MainActivity.this, "nn", Toast.LENGTH_LONG).show();
break;
case R.id.query_data:
SQLiteDatabase db2=dbHelper.getWritableDatabase();
Cursor cursor=db2.query("ls",null,null,null,null,null,null,null);
if(cursor.moveToFirst()){
do{
int age=cursor.getInt(cursor.getColumnIndex("age"));
Log.d("MainActivity","ls age is"+age);
}while(cursor.moveToNext());
}
cursor.close();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/create_database"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create database"
/>
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"
/>
<Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Query data"
/>
</LinearLayout>