关于SQLiteDatabase

用户收藏网站的一个小程序:

 用户输入数据类:

package com.xxg.favorites;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FavoritesActivity extends Activity {
	//实例化EditText
	private EditText et1,et2,et3;
	private Button b1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        this.setTitle("添加收藏信息");
        et1=(EditText)findViewById(R.id.EditTextName);
        et2=(EditText)findViewById(R.id.EditTextUrl);
        et3=(EditText)findViewById(R.id.EditTextDesc);
        b1=(Button)findViewById(R.id.ButtonAdd);
        
        b1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String name=et1.getText().toString();
				String url=et2.getText().toString();
				String desc=et3.getText().toString();
				ContentValues values=new ContentValues();
				values.put("name", name);
				values.put("url",url);
				values.put("desc",desc);
				DBHelper helper=new DBHelper(getApplicationContext());
				helper.insert(values);
				
				Intent intent=new Intent(FavoritesActivity.this,QueryActivity.class);
				startActivity(intent);
			}
        	
        });
    } 
   }

数据显示类:

package com.xxg.favorites;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class QueryActivity extends ListActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setTitle("浏览收藏信息");
		//final DBHelper helpter=new DBHelper(this);
		final DBHelper helpter=new DBHelper(getApplicationContext());
		//获得游标
		Cursor c=helpter.query();
		String[] from={"_id","name","url","desc"};
		int[] to={R.id.text0,R.id.text1,R.id.text2,R.id.text3};
		SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.row,c,from,to);
		
		ListView listview=getListView();
		listview.setAdapter(adapter);
		
		final AlertDialog.Builder builder=new AlertDialog.Builder(this);
		
		listview.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				final long temp=arg3;
				builder.setMessage("真的要删除记录嘛?").setPositiveButton("是", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						helpter.del((int)temp); 
						Cursor c=helpter.query();
						String[] from={"_id","name","url","desc"};
						int[] to={R.id.text0,R.id.text1,R.id.text2,R.id.text3};
						SimpleCursorAdapter adapter=new SimpleCursorAdapter(getApplicationContext(),R.layout.row,c,from,to);
						ListView listview=getListView();
						listview.setAdapter(adapter);
					}
				}).setNegativeButton("否",new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
					
				});
				AlertDialog ad=builder.create();
				ad.show();
			}
			
		});
		helpter.close();
	}

}

数据库帮助类:

package com.xxg.favorites;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	//数据库名称
	private static final String DB_NAME="coll01.db";
	//表名
	private static final String TBL_NAME="CollTbl01";
	//创建SQL语句
	private static final String CREATE_TBL="create table CollTbl01(_id integer primary key autoincrement,name text,url text,desc text)";
	//SQLiteDatabase实例
	private SQLiteDatabase db;
	//构造方法
	public DBHelper(Context c) {
		super(c, DB_NAME, null,2);
		// TODO Auto-generated constructor stub
	}
//创建表
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		this.db=db;
		db.execSQL(CREATE_TBL);
	}
	//插入方法
public void insert(ContentValues values){
	//获得SQLiteDataBase实例
    SQLiteDatabase db=getWritableDatabase();
    //插入
    db.insert(TBL_NAME, null, values);
    db.close();
}
//查询方法
public Cursor query(){
	//获取SQLiteDatabase实例
	SQLiteDatabase db=getWritableDatabase();
	//查询获得Cursor
	Cursor c=db.query(TBL_NAME, null, null, null, null, null, null);
	return c;
}
//删除方法
public void del(int id){
	if(db==null)
		//获取SQLiteDatabase
		db=getWritableDatabase();
		db.delete(TBL_NAME,"_id=?",new String[]{String.valueOf(id)});
	
}
//关闭数据库
public void close(){
	if(db!=null){
		db.close();
	}
}
	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

}


问题是:刚开始在数据库帮助类中
CREATE_TBL="create table CollTbl01(_id integer primary key autoincrement,name text,desc text)";
创建表的语句写错了,把url text写漏了,运行程序始终出现问题,找不到url无法启动数据显示类,后来改为:
CREATE_TBL="create table CollTbl01(_id integer primary key autoincrement,name text,url text,desc text)";
依旧报同样的错误!
最后修改表名在运行问题依旧
知道修改了数据库的名字在运行成功!求解.................??



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值