我的主文件 它调用dbhelper和datalist视图类
package com.example.insertdatafinal;
//import android.support.v7.app.ActionBarActivity;
//mport com.example.insertdata2.DBHelper;
//import com.example.insertdata2.DataListView;
//mport com.example.insertdata2.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener{
private Button but1;
private Button but2;
//private String tableName = DBHelper.tableName;
private SQLiteDatabase newDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1=(Button)findViewById(R.id.button1);
but1.setOnClickListener(this);
but2=(Button)findViewById(R.id.button2);
but2.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View arg0)
{
switch(arg0.getId())
{
case R.id.button1:
{
EditText a2;
EditText a3;
EditText a4;
EditText a5;
a2=(EditText)findViewById(R.id.edit1);
a3=(EditText)findViewById(R.id.edit2);
a4=(EditText)findViewById(R.id.edit3);
a5=(EditText)findViewById(R.id.edit4);
String name=a2.getText().toString();
String age=a3.getText().toString();
String place=a4.getText().toString();
String occu=a5.getText().toString();
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
dbHelper.insertNew(name,age,place,occu);
//,place,occu);
Toast.makeText(this,"working", Toast.LENGTH_LONG).show();
//Toast.makeText(this,name, Toast.LENGTH_LONG).show();
//Toast.makeText(this,age, Toast.LENGTH_LONG).show();
break;
}
case R.id.button2:
{
final Context context=this;
Intent intent= new Intent(context, DataListView.class);
startActivity(intent);
break;
}
}
}
}`
我的dbhelper文件,用于插入我认为正常工作的数据
`public class DBHelper extends SQLiteOpenHelper{
public SQLiteDatabase DB;
public String DBPath;
public static String DBName = "sample";
public static final int version = '1';
public static Context currentContext;
public static String tableName = "Resource";
public DBHelper(Context context) {
super(context, DBName, null, version);
currentContext = context;
//DBPath = "/data/data/" + context.getPackageName() + "/databases";
createDatabase();
private void createDatabase() {
//boolean dbExists = checkDbExists();
/*if (dbExists) {
// do nothing
} else {*/
DB = currentContext.openOrCreateDatabase(DBName, 0, null);
DB.execSQL("CREATE TABLE IF NOT EXISTS " +
tableName +
" ( FirstName VARCHAR, Age INT(3),place VARCHAR,occupation VARCHAR );");
}
public void insertNew(String name, String age, String place,String occu) {
// TODO Auto-generated method stub
//boolean dbExists = checkDbExists();
/*//if (dbExists) {
// do nothing
} else {*/
DB = currentContext.openOrCreateDatabase(DBName, 0, null);
DB.execSQL("INSERT INTO " +
tableName +
" Values ('"+name+"','"+age+"','"+place+"','"+occu+"');"); `
和我的datalist视图文件在打开类时出错
`
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
TextView tView = new TextView(this);
tView.setText("This data is retrieved from the database and only 10 " +
"of the results are displayed");
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
//
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
//FirstName, Age, place, occupation
Cursor c = newDB.rawQuery("SELECT * FROM " +
tableName +
" where Age>10 ", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
int age = c.getInt(c.getColumnIndex("Age"));
String pla = c.getString(c.getColumnIndex("place"));
String occu = c.getString(c.getColumnIndex("occupation"));
//String place,occu;
results.add("Name: " + firstName + ",Age: " + age+pla+occu);
//+"place: "+place+"occu:"+occu);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (newDB != null)
newDB.execSQL("DELETE FROM " + tableName);
newDB.close();
}`
请帮助我们数据正确输入但是当我点击查看数据时它显示错误并关闭。