Wednesday, May 1, 2013

[android-developers] Re: SQLite Frustration

It appears to me that you are always inserting key_id = 0, therefore this would be a duplicate insert.  I believe you should be increasing this by +1 for each insert.

On Friday, April 26, 2013 11:20:20 AM UTC-5, Nathan wrote:
I can not figure out what is happening here for the life of me. Everything was working fine. I went to bed one night and came back the next and now my app will not read or write to my db and gives me no errors. Please help any advice would be great.


My DB Handler:
===============================================================================================================
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "GP";
 
    // Contacts table name
    private static final String TABLE_VERSION = "GPVersion";
    
 
    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_VERSION = "name";
    
public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
        String CREATE_VERSION_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_VERSION + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_VERSION + " TEXT" + ")";
        db.execSQL(CREATE_VERSION_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
        //db.execSQL("DROP TABLE IF EXISTS " + TABLE_VERSION);
 
        // Create tables again
        //onCreate(db);
}

// Adding new version 
    void addGPVersion(String version) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_ID, "0"); // Pair
        values.put(KEY_VERSION, version); // Pair
 
        // Inserting Row
        db.insert(TABLE_VERSION, null, values);
        db.close(); // Closing database connection
    }
    
    
    // Getting students Count
    public String getGPVersion() {
    SQLiteDatabase db = this.getReadableDatabase();
    String q="SELECT " + KEY_VERSION + " FROM " + TABLE_VERSION + " WHERE " + KEY_ID + "='0'";
    String z;
   
    Cursor cursor = db.rawQuery(q,null);
    if (cursor != null && cursor.getCount()>0) {
    cursor.moveToFirst();
    z=cursor.getString(0);
    cursor.close();
        db.close();
        return z;
   
    }
    else
    {
    cursor.close();
    db.close();
        return "na";
    }
    }
    

===============================================================================================================



Main Activity:
===============================================================================================================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
       CheckGPVersion();
}


    private void CheckGPVersion()
    {
    DatabaseHandler db = new DatabaseHandler(this);
         String a = "";
         a = db.getGPVersion();

         EditText txtEffortCode = (EditText) findViewById(R.id.txtEffortCode);
         txtEffortCode.setText(a);
}


public void onGoClick(View view)  // Input Effort Code
    {  
EditText txtEffortCode = (EditText) findViewById(R.id.txtEffortCode);

    if (txtEffortCode.getText().length() != 0){
       //Do Something
    DatabaseHandler db = new DatabaseHandler(this);
    db.addGPVersion(txtEffortCode.getText().toString().toUpperCase(Locale.getDefault()));
    txtEffortCode.setText("");
    Toast.makeText(this, "Input Success!", Toast.LENGTH_SHORT).show();
    }
    else
    {
    //Alert no code input
    Toast.makeText(this, "You must enter 'EFFORT CODE'!", Toast.LENGTH_SHORT).show();
    }  
    }  
===============================================================================================================


While it creates the database or so I think it does as the app shows under data 4.00K. When it was inputting data the other night, the data field showed 12.00K. Argh, what is going on here? I use sqlite on many other apps and they work fine. Did something get corrupt? I tried cleaning the project, rebooting etc. nothing. Thanks in advance.

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment