所以,这里有一个问题,我有两个班级,Class Title一个人的信息,即姓名,地址,网站,电话等,当人选择保存按钮时,他所有的个人资料都保存在SQlite中,他是转移到Class Newscene,它有一个编辑文本视图,人们可以在其中编写故事。当他写这个故事时,我希望它能够与最近保存的标题的ID保存在一起。但不是这样做,程序崩溃并给我错误。我的标题类的代码,我从中插入一个人的详细信息到数据库
EditText title=(EditText)findViewById(R.id.editText1);
EditText author=(EditText)findViewById(R.id.editText2);
EditText company=(EditText)findViewById(R.id.editText3);
EditText address=(EditText)findViewById(R.id.editText4);
EditText email=(EditText)findViewById(R.id.editText5);
EditText website=(EditText)findViewById(R.id.editText6);
EditText phone=(EditText)findViewById(R.id.editText7);
title1=title.getText().toString();
author1=author.getText().toString();
company1=company.getText().toString();
address1=address.getText().toString();
email1=email.getText().toString();
website1=website.getText().toString();
phone1=phone.getText().toString();
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact(0, title1, author1,company1, address1, email1, website1, phone1, script1));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.get_name() + " ,Author: " + cn.get_author() + " ,company: " + cn.get_company() + " ,address: " + cn.get_address()
+ " ,email: " + cn.get_email() + " ,Website: " + cn.get_website() + " ,Phone: " + cn.getPhoneNumber() + " ,Script: " + cn.getscript();
// Writing Contacts to log
Log.d("Name: ", log);
name_title=cn.getName().toString();
}
//Passing id through intent
Intent i = new Intent(Title.this,Newscene.class);
i.putExtra("EXTRA_ID", id );
startActivity(i);
这是我在Newscene Class中获取它的地方
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newscene);
Intent i = getIntent();
idval = i.getStringExtra("EXTRA_ID").toString();
// i am converting this idval string to integer below
}
然后在同一个班级中,一个人在其立即保存在
中的edittext中写下的所有内容 String strSaveindb = previous + scene1 + ending;
然后我在我的Contact类中设置它并像这样更新数据库(这是在Newscene活动中完成的)
cn.setscript(strSaveindb);
db.updateContactscript(db.getContact(1));
现在在我的Databasehandler类中,我有updatecontact函数和updatecontactscript函数,以下是我的数据库处理程序类的代码
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_AUTHOR + " TEXT,"
+ KEY_COMPANY + " TEXT,"
+ KEY_ADDRESS + " TEXT,"
+ KEY_EMAIL + " TEXT,"
+ KEY_WEBSITE + " TEXT,"
+ KEY_PH_NO + " TEXT,"
+ KEY_SCRIPT + "TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
这是添加联系人功能
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_AUTHOR, contact.getPhoneNumber());
values.put(KEY_COMPANY, contact.getPhoneNumber());
values.put(KEY_ADDRESS, contact.getPhoneNumber());
values.put(KEY_EMAIL, contact.getPhoneNumber());
values.put(KEY_WEBSITE, contact.getPhoneNumber());
values.put(KEY_PH_NO, contact.getPhoneNumber());
values.put(KEY_SCRIPT, contact.getscript());// Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
这是get conatct函数
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_AUTHOR, KEY_COMPANY, KEY_ADDRESS, KEY_EMAIL, KEY_WEBSITE, KEY_PH_NO , KEY_SCRIPT }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7),cursor.getString(8));
// return contact
return contact;
}
这些是更新联系人和更新脚本</ p>的功能
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_AUTHOR, contact.get_author());
values.put(KEY_COMPANY, contact.get_company());
values.put(KEY_ADDRESS, contact.get_address());
values.put(KEY_EMAIL, contact.get_email());
values.put(KEY_WEBSITE, contact.get_website());
values.put(KEY_PH_NO, contact.getPhoneNumber());
values.put(KEY_SCRIPT, contact.getscript());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
//UPDATING SCRIPT
public int updateContactscript(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SCRIPT, contact.getscript());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
如何更新最近制作的联系人ID的脚本。我现在被困在这里两天了:(
答案 0 :(得分:1)
修正此行:
KEY_SCRIPT + "TEXT"
到
KEY_SCRIPT + " TEXT"