SQLite数据库作业

安卓作业:

a)将学生信息存入数据库

b)显示所有学生信息列表

c)删除数据库表中的一条信息

 1.xml界面

   主界面

<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.edu.niit.sqlite.MainActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:id="@+id/w"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick"
android:layout_height="wrap_content"
android:text="增加一条新记录呀"
android:id="@+id/add"/>
<Button
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="onClick"
android:layout_height="wrap_content"
android:text="删除第一条记录呀"
android:id="@+id/delete"/>
</LinearLayout>

<ListView
android:layout_width="match_parent"
android:id="@+id/t"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>

2.创建数据库

public class MyDBHelper extends SQLiteOpenHelper {
private static final String name = "count";
private static final int version = 1;
private String sql = "create table person(_id int autoincrement primary key ," +
"name varchar(30) not null)";

public MyDBHelper(Context context) {
super(context, name, null, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists person");
onCreate(db);
}
3.执行删除与添加语句
public class MyDBHelper extends SQLiteOpenHelper {
private static final String name = "count";
private static final int version = 1;
private String sql = "create table person(_id int autoincrement primary key ," +
"name varchar(30) not null)";

public MyDBHelper(Context context) {
super(context, name, null, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists person");
onCreate(db);
}
}
4.增加Adapter方法
private void addAdp() {
if(c!=null){
dao = new PersonDAO(this);
c = dao.selectAll();
adp = new MyCursorAdpter(this,c,0);
lv.setAdapter(adp);
}
}
5.java代码的实现
public class MainActivity extends AppCompatActivity {
private EditText et_name;
private ListView data;
private Adapter adapter;
private StudentDAO studentDAO;
private Cursor cursor;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_name = (EditText)findViewById(R.id.et_name);

data = (ListView)findViewById(R.id.data);
//查询数据并进行显示
studentDAO = new StudentDAO(this);
cursor = studentDAO.selectAll();
if(cursor!=null){
Adapter = new Adapter(MainActivity.this,cursor);
data.setAdapter(adapter);
}


}

public void onClick(View view) {
switch(view.getId()){
case R.id.add:
//add数据
String name = et_input.getText().toString();
personDAO.insert(name);
cursor = personDAO.selectAll();
if(cursor!=null){
adapter = new Adapter(this,cursor);
data.setAdapter(adapter);
}
break;
case R.id.delete:
//delete数据
if(cursor!=null){
if(cursor.moveToFirst()){
studentDAO.delete(cursor.getString(cursor.getColumnIndex("_id")));
cursor = studentDAO.selectAll();
}
}
adapter = new Adapter(this,cursor);
data.setAdapter(adapter);
break;
}

 
 
posted @ 2017-05-16 22:26  元气七七  阅读(209)  评论(0编辑  收藏  举报