Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating some columns of database in android SQlite
    primarykey
    data
    text
    <p>So, here is the question, I have two classes, Class Title the information of a person, i.e name, address, website, phone etc and when the person selects save button all his personal data is saved in SQlite and he is moved to Class Newscene which have a edit text view on which the person writes a story. and as he writes the story, i want it to get saved against the id of the title which has been saved recently. but instead of doing it, the program crashes and gives me error.here is the code of my title class from which i am inserting the details of a person into database</p> <pre><code>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&lt;Contact&gt; 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); </code></pre> <p>This is where i am getting it in Newscene Class</p> <pre><code> 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 } </code></pre> <p>then in same class, everything a person writes in the edittext it saves immediately in </p> <pre><code> String strSaveindb = previous + scene1 + ending; </code></pre> <p>and then i am setting this in my Contact class and updating database like this ( this is being done in Newscene activity )</p> <pre><code> cn.setscript(strSaveindb); db.updateContactscript(db.getContact(1)); </code></pre> <p>Now in my Databasehandler class i have updatecontact function and updatecontactscript function, following is the code of my database handler class</p> <pre><code>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); } </code></pre> <p>tHIS IS THE ADD CONTACT FUNCTION </p> <pre><code>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 } </code></pre> <p>This is the get conatct function</p> <pre><code> 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; } </code></pre> <p>And these are the functions to update contact, and update only script</p> <pre><code> // 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()) }); } </code></pre> <p>How Can i update the script infront of the id of the contact which have recently been made. i am stuck here for two days now :(</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload