首页 > 代码库 > SQLite

SQLite

建立一个音乐列表,添加音乐能够用ListView显示出来。

添加音乐界面代码:

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	TextView tet1,tet2;
	EditText edt1,edt2;
	Button btn;
	int verson=2;//数据库版本
	String dbname="sql_db";//表名
	Cursor cr;
	ContentValues values=new ContentValues();
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tet1=(TextView)findViewById(R.id.tet1);
		tet2=(TextView)findViewById(R.id.tet2);
		edt1=(EditText)findViewById(R.id.edt1);
		edt2=(EditText)findViewById(R.id.edt2);
		btn=(Button)findViewById(R.id.btn);
		DatebaseHelper dbh=new DatebaseHelper(this, dbname,null,verson);
		final SQLiteDatabase sqldb=dbh.getWritableDatabase();
		btn.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				cr=sqldb.query("musiclist", null, null, null, null, null, null);
				int count=1;
				while(cr.moveToNext()){count++;
				System.out.println(count);
				}
				values.put("num", count);
				values.put("name", "  "+edt1.getText().toString());
				values.put("singer", "  "+edt2.getText().toString());
				long res=sqldb.insert("musiclist", null, values);//添加到数据库
				//int res=sqldb.delete("musicList", null, null);//删除数据库中的信息
				if(res==-1){
					Toast.makeText(MainActivity.this, "添加失败",Toast.LENGTH_SHORT).show();  

				}else{
					Toast.makeText(MainActivity.this, "添加成功",Toast.LENGTH_SHORT).show(); 
				}
				Intent intent=new Intent(MainActivity.this,SecondActivity.class);
				startActivity(intent);
			}
		});
	}

}

布局:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tet1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="音乐名"
         />
    <EditText
        android:id="@+id/edt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tet1"
        android:layout_alignLeft="@+id/tet1"
        />
     <TextView
        android:id="@+id/tet2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edt1"
        android:layout_alignLeft="@+id/tet1"
        android:text="歌手"
         />
    <EditText
        android:id="@+id/edt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tet2"
        android:layout_alignLeft="@+id/tet1"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edt2"
        android:layout_alignLeft="@+id/tet1"
        android:text="添加"
        />

</RelativeLayout>

显示列表界面代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SecondActivity extends Activity{
	SQLiteDatabase sdb;
	int verson=2;//数据库版本
	String name="sql_db";//表名
	ListView lv;
	List<Map<String,Object>>list;
	Cursor cr;
	Database db;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		lv=(ListView)findViewById(R.id.lv);
		list=new ArrayList<Map<String,Object>>();
		db=new Database(this, name,null,verson);
		sdb=db.getReadableDatabase();
		cr=sdb.query("musiclist", null, null, null, null, null, null);//查询表
		while(cr.moveToNext()){
			Map<String, Object>mp=new HashMap<String, Object>();
			mp.put("num", cr.getInt(0));
			mp.put("name", cr.getString(1));
			mp.put("singer", cr.getString(2));
			list.add(mp);
			SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.layout_listview, new String[]{"num","name","singer"},new int[]{R.id.num,R.id.name,R.id.singer}); //显示到list上 
			lv.setAdapter(adapter);
		}
	}
}

布局:

<?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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="浏览音乐信息" />

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

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="horizontal" >

    <TextView
        android:id="@+id/num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/singer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

连接数据库部分1:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatebaseHelper extends SQLiteOpenHelper{
    public static final int i=2;
	public DatebaseHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
	public DatebaseHelper(Context context,String name,int version){
		this(context,name,null,version);
	}
	public DatebaseHelper(Context context,String name){
		this(context,name,i);
	}

	public void onCreate(SQLiteDatabase db) {
		System.out.print("Create a Database");
		db.execSQL("create table musicList(num int,name verchar(20),singer verchar(10))");
	}

	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		
	}
}

连接数据库部分2:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public  class Database extends SQLiteOpenHelper{
    static int i=2;
	public Database(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);	
	}
	public Database(Context context,String name,int version){
		this(context,name,null,version);
	}
	public Database(Context context,String name){
		this(context,name,i);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub	
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub	
	}
}