开发者社区> 问答> 正文

Android SQLite相关的 帮帮忙解决一下? 400 报错

Android SQLite相关的 帮帮忙解决一下? 400 报错

重要的几个文件SQLiteHelper.java

package com.eclipghc.sqlite;

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

public class SQLiteHelper extends SQLiteOpenHelper {

	private static final String DB_NAME = "brushdrill.db";
	private static final String TBL_NAME = "tbl_bdrill";
	private static final String CONTENT = "content", NUNMBER = "number", _ID = "_ID";
	public SQLiteHelper(Context context) {
		super(context, DB_NAME, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table " + TBL_NAME + " (" + _ID
				+ " integer primary key autoincrement, " + CONTENT
				+ " text, " + NUNMBER + " text);";
		db.execSQL(sql);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "DROP TABLE IF EXISTS " + TBL_NAME;
		db.execSQL(sql);
		onCreate(db);
	}
	
//	添加方法
	public void insert(String content, String number) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(CONTENT, content);
		values.put(NUNMBER, number);
		db.insert(TBL_NAME, null, values);
		db.close();
	}
	
//	查询方法
	public Cursor query() {
		SQLiteDatabase db = getWritableDatabase();
		Cursor cursor = db.query(TBL_NAME, null, null, null, null, null, null);
		return cursor;
	}

//	删除方法
	public void delete(int id) {
		SQLiteDatabase db = getWritableDatabase();
		String where = _ID + " = ? ";
		String[] whereValue =  { Integer.toString(id) };
		db.delete(TBL_NAME, where, whereValue);
	}
	
//	修改方法
	public void update(int id, String content, String number) {
		SQLiteDatabase db = getWritableDatabase();
		String where = _ID + " = ? ";
		String[] whereValue = { Integer.toString(id)};
		ContentValues values = new ContentValues();
		values.put(CONTENT, content);
		values.put(NUNMBER, number);
		db.update(TBL_NAME, values, where, whereValue);
	}
}
MainActivity.java


package com.eclipghc.brushdrill;

import com.eclipghc.sqlite.SQLiteHelper;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {

	private Button addButton, outButton;
	private ListView listView;
	private EditText editText1, editText2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addButton = (Button) findViewById(R.id.add_button);
        
        addButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final SQLiteHelper helper = new SQLiteHelper(MainActivity.this);
				View view = getLayoutInflater().inflate(R.layout.alertdialog_main_context, null);
				editText1 = (EditText) view.findViewById(R.id.editText1);
				editText2 = (EditText) view.findViewById(R.id.editText2);
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle(R.string.add_button);
				builder.setIcon(android.R.drawable.ic_menu_add);
				builder.setView(view);
				builder.setPositiveButton(R.string.yes, new android.content.DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						String content = editText1.getText().toString();
						String number = editText2.getText().toString();
						if (content.equals("") || number.equals("")) {
							return;
						}
						helper.insert(content, number);
					}
				});
				builder.setNegativeButton(R.string.no, null);
				builder.show();
			}
		});
        
        outButton = (Button) findViewById(R.id.out_button);
        
        outButton.setOnClickListener(null);
        
        listView = (ListView) findViewById(android.R.id.list);
        SQLiteHelper helper = new SQLiteHelper(MainActivity.this);
        Cursor c = helper.query();
        String[] from = {"content","number"};
        int[] to = {R.id.textViewContent,R.id.textViewNumber};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.list_main, c, from, to);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}
logCat 总是报错  SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.list_main, c, from, to); 这句不知道那里出错,帮帮忙啊?还在学习,不要见笑?


展开
收起
优选2 2020-06-09 11:19:11 523 0
1 条回答
写回答
取消 提交回答
  • 怎么没人回答呢?帮帮我,该怎么做,那里错了啊######你应该把错误信息贴出来######你看看log,里面有个_id列不存在的错误,意思是你这个table里面没有这一列啊,仔细检查下代码。######因为SimpleCursorAdapter继承ResourceCursorAdapter,ResourceCursorAdapter继承CursorAdapter,CursorAdapter的构造函数中有这么一段c.getColumnIndexOrThrow("_id")代码,如果table中没有名为_id的列,则会抛出异常。你的问题就在此,在你的table中加上名为_id的列就OK了######"_ID"这个应该换成小写

    2020-06-09 11:19:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载