开发者

How to check if a row in a table exists?

开发者 https://www.devze.com 2023-02-09 19:27 出处:网络
What i\'m trying to do:ListView activity A, I touch an item, it opens a new listview activity.I take the primary key from the database row selected in list A and use putExtra. In activity B, in onCrea

What i'm trying to do: ListView activity A, I touch an item, it opens a new listview activity. I take the primary key from the database row selected in list A and use putExtra. In activity B, in onCreate I want to check and see if there is any row in Table B where the column TOPIC_CONTENT_TOPIC_ID has a value that equals the primary key from table A that was putExtra. If the table is empty, or if there is no matching ID, i want to create a new row where column TOPIC_CONTENT_TOPIC_ID now equals the primary key from table A. Here is my code. It crashes on the db.query();

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.topic_content);

    dbMaker = new DatabaseMaker(this);
    SQLiteDatabase db = dbMaker.getReadableDatabase();

    Intent myIntent = getIntent();
    int topicIDInt = (int) myIntent.getLongExtra("com.spentakapps.ScripturalConcordance.Topics", -1);
    String topicID = Long.toString(myIntent.getLongExtra("com.spentakapps.ScripturalConcordance.Topics", -1));

    String WHERE = DatabaseStructure.TOPICS_CONTENT_TOPIC_ID + " = " + topicID;
    Cursor cur = db.query(DatabaseStructure.TABLE_TOPICS_CONTENT, new String[] {DatabaseStructure.TOPICS_CONTENT_CONTENT},WHERE, null, null, null, null);

    if (cur.getCount() <= 0)
    {
        ContentValues values = new ContentValues();
        values.put(DatabaseStructure.TOPICS_CONTENT_TOPIC_ID, topicIDInt);
        db = dbMaker.getWritableDatabase();
        db.insert(DatabaseStructure.TABLE_TOPICS_CONTENT, null, values);
       开发者_如何学运维 db = dbMaker.getReadableDatabase();
        cur = db.query(DatabaseStructure.TABLE_TOPICS_CONTENT, new String[] {DatabaseStructure.TOPICS_CONTENT_TOPIC_ID},WHERE, null, null, null, null);
    }

    View addContentButton = findViewById(R.id.content_button_add);
    addContentButton.setOnClickListener(this);

    //Set up data binding
    SimpleCursorAdapter  adapter = new SimpleCursorAdapter(this,R.layout.topiccontentlineitem,cur, FROM,TO);
    setListAdapter(adapter);

}


My recommendation for these sorts of issues is always to log into the emulator using:

adb shell

then open the sqlite db directly

# cd /data/data/<your application package>/databases/
# ls
# sqlite3 <your database file>

once inside you should be able to try to manually run your sql to make sure it works:

select id from TABLE_TOPICS_CONTENT where id='someidvalue' limit 1

When you say it crashes on db.query, there are two db.query statemenets in your above code. My guess is that you are running into significant problems since you are calling dbMaker.getReadableDatabase(); several times. Try to just call it once in the beginning as runnable and try to verify that your sql is valid using those. If that fails, attach your error log.

0

精彩评论

暂无评论...
验证码 换一张
取 消