这是我的数据库帮助程序类的代码,任何人都可以告诉我,我在做什么错误,表中没有添加电子邮件列,每次我收到错误,表没有cloumn名称person_email
package com.example.and;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class AndroidOpenDbHelper {
private static final String TAG = AndroidOpenDbHelper.class.getSimpleName();
// database configuration
// if you want the onUpgrade to run then change the database_version
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydatabase.db";
// table configuration
private static final String TABLE_NAME = "person_table"; // Table name
private static final String PERSON_TABLE_COLUMN_ID = "_id"; // a column named "_id" is required for cursor
private static final String PERSON_TABLE_COLUMN_NAME = "person_name";
private static final String PERSON_TABLE_COLUMN_NUMBER = "person_contact_number";
private static final String PERSON_TABLE_COLUMN_EMAIL = "person_email";
private DatabaseOpenHelper openHelper;
private SQLiteDatabase database;
// this is a wrapper class. that means, from outside world, anyone will communicate with PersonDatabaseHelper,
// but under the hood actually DatabaseOpenHelper class will perform database CRUD operations
public AndroidOpenDbHelper(Context aContext) {
openHelper = new DatabaseOpenHelper(aContext);
database = openHelper.getWritableDatabase();
}
public void insertData (String aPersonName, String aPersonContactNumber, String aPersonEmail) {
// we are using ContentValues to avoid sql format errors
ContentValues contentValues = new ContentValues();
contentValues.put(PERSON_TABLE_COLUMN_NAME, aPersonName);
contentValues.put(PERSON_TABLE_COLUMN_NUMBER, aPersonContactNumber);
contentValues.put(PERSON_TABLE_COLUMN_EMAIL, aPersonEmail);
database.insert(TABLE_NAME, null, contentValues);
}
public Cursor getAllData () {
String buildSQL = "SELECT * FROM " + TABLE_NAME;
Log.d(TAG, "getAllData SQL: " + buildSQL);
return database.rawQuery(buildSQL, null);
}
// this DatabaseOpenHelper class will actually be used to perform database related operation
private class DatabaseOpenHelper extends SQLiteOpenHelper {
public DatabaseOpenHelper(Context aContext) {
super(aContext, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// Create your tables here
String buildSQL = "CREATE TABLE " + TABLE_NAME + "( " + PERSON_TABLE_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
PERSON_TABLE_COLUMN_NAME + " TEXT, " + PERSON_TABLE_COLUMN_NUMBER + " TEXT " +")" ;
//+ PERSON_TABLE_COLUMN_EMAIL + "TEXT )";
Log.d(TAG, "onCreate SQL: " + buildSQL);
sqLiteDatabase.execSQL(buildSQL);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
// Database schema upgrade code goes here
String buildSQL = "DROP TABLE IF EXISTS " + TABLE_NAME;
Log.d(TAG, "onUpgrade SQL: " + buildSQL);
sqLiteDatabase.execSQL(buildSQL); // drop previous table
onCreate(sqLiteDatabase); // create the table from the beginning
}
}
}
这是我的MainActivity类
package com.example.and;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
private DataCursorAdapter customAdapter;
private AndroidOpenDbHelper databaseHelper;
private static final int ENTER_DATA_REQUEST_CODE = 1;
private ListView listView;
private static final String TAG = MainActivity.class.getSimpleName();
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new AndroidOpenDbHelper(this);
listView = (ListView) findViewById(R.id.list_data);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, AddNewUndergraduateActivity.class);
// intent.putExtra("number",itemclicked );
startActivity(intent);
//Log.d(TAG, "clicked on item: " + position);
}
});
// Database query can be a time consuming task ..
// so its safe to call database query in another thread
// Handler, will handle this stuff for you <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g" alt=":)" class="wp-smiley">
new Handler().post(new Runnable() {
@Override
public void run() {
customAdapter = new DataCursorAdapter(MainActivity.this, databaseHelper.getAllData());
listView.setAdapter(customAdapter);
}
});
}
public void onClickEnterData(View btnAdd) {
startActivityForResult(new Intent(this, AddNewUndergraduateActivity.class), ENTER_DATA_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ENTER_DATA_REQUEST_CODE && resultCode == RESULT_OK) {
databaseHelper.insertData(data.getExtras().getString("tag_person_name"), data.getExtras().getString("tag_person_contact_number"),
data.getExtras().getString("tag_person_email")
);
customAdapter.changeCursor(databaseHelper.getAllData());
}
}
}
现在我想知道如何将所选项目传递给其他活动,并根据所选项目从数据中获取数据。因为我的列表项是一个自定义的光标对象。所以,我不知道该怎么做。
这是我的光标适配器
package com.example.and;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class DataCursorAdapter extends CursorAdapter {
public DataCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.listcell, parent, false);
return retView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
TextView textViewPersonPIN = (TextView) view.findViewById(R.id.tv_person_pin);
textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
TextView textViewPersonEMAIL = (TextView) view.findViewById(R.id.tv_person_email);
textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));
}
}
答案 0 :(得分:7)
您已注释掉创建该列的代码:
//+ PERSON_TABLE_COLUMN_EMAIL + "TEXT )";
您还需要在列名称及其类型TEXT
之间留出空格,例如
+ PERSON_TABLE_COLUMN_EMAIL + " TEXT )";
编辑SQLite助手onCreate()
后,卸载您的应用,以便再次运行onCreate()
。有关详情,请参阅When is SQLiteOpenHelper onCreate() / onUpgrade() run?。
答案 1 :(得分:5)
删除评论并更正您的查询,如下所示:
String buildSQL = "CREATE TABLE " + TABLE_NAME + "(" + PERSON_TABLE_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
PERSON_TABLE_COLUMN_NAME + " TEXT, " + PERSON_TABLE_COLUMN_NUMBER + " TEXT,"
+ PERSON_TABLE_COLUMN_EMAIL + " TEXT)";
重新安装了您的应用。