SQLite CRUD问题

时间:2015-02-23 17:10:55

标签: java android sqlite

我试图在vogella教程http://www.vogella.com/tutorials/AndroidSQLite/article.html的基础上实现一个简单的SQLite数据库。

下面的数据库很简单,我只有一列COLUMN_TOPIC,我设置为TEXT PRIMARY KEY NOT NULL。

我遇到的问题是关于我所提出的CRUD操作。我无法使用

行检索单个主题对象
Cursor cursor = database.query(TopicDBHelper.TABLE_TOPICS, allColumns,
      TopicDBHelper.COLUMN_TOPIC + " = " + "'" + topic + "'", null, null, null, null); if (cursor
      != null && cursor.moveToFirst()) { newTopic = cursorToTopic(cursor);

我无法使用getAllTopics()方法检索所有主题。返回的值为NULL

根据我的发现,可以使用String或Text作为主键,但不一定建议使用。

我尝试访问数据库的方式有什么不对吗?或者我正在尝试执行的SQL有问题吗?

这就是我调用相关数据库操作的方法

MQTTServiceDelegate.addTopicToDB(this, "ALL");
MQTTServiceDelegate.getAllTopicsFromDB(this);

这是 MQTTServiceDelegate

    public static void addTopicToDB(Context context, String topicName) {
    TopicsDataSource datasource = new TopicsDataSource(context);
    datasource.open();
    datasource.createTopic(topicName);
    datasource.close();
  }

  public static void removeTopicFromDB(Context context, String topicName) {
    TopicsDataSource datasource = new TopicsDataSource(context);
    datasource.open();
    datasource.deleteTopic(topicName);
    datasource.close();
  }

  public static void getAllTopicsFromDB(Context context){
    TopicsDataSource datasource = new TopicsDataSource(context);
    datasource.open();
    datasource.getAllTopics();
    datasource.close();
  }

TopicDBHelper

  public static final String TABLE_TOPICS = "topics";
  public static final String COLUMN_TOPIC = "topic";
  public static final String COLUMN_TOPIC_ID = "topic_id";

  private static final String DATABASE_NAME = "topics.db";
  private static final int DATABASE_VERSION = 1;


  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_TOPICS + "(" + COLUMN_TOPIC
      + " text primary key not null);";



  public TopicDBHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(DATABASE_CREATE);

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    Log.w(TopicDBHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPICS);
    onCreate(db);  
  }

TopicDataSource

// Database fields
  private SQLiteDatabase database;
  private TopicDBHelper dbHelper;
  private String[] allColumns = {TopicDBHelper.COLUMN_TOPIC};

  public TopicsDataSource(Context context) {
    dbHelper = new TopicDBHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public void createTopic(String topic) {
    Topic newTopic = null;
    ContentValues values = new ContentValues();
    values.put(TopicDBHelper.COLUMN_TOPIC, topic);
    /*
     * Cursor cursor = database.query(TopicDBHelper.TABLE_TOPICS, allColumns,
     * TopicDBHelper.COLUMN_TOPIC + " = " + "'" + topic + "'", null, null, null, null); if (cursor
     * != null && cursor.moveToFirst()) { newTopic = cursorToTopic(cursor); cursor.close();
     * Log.e("CURSOR", newTopic.get_topicName());
     * 
     * } Log.e("CURSOR", "" + newTopic.get_topicName());
     */
    /* return newTopic; */

  }

  public void deleteTopic(String topicName) {
    database.delete(TopicDBHelper.TABLE_TOPICS, TopicDBHelper.COLUMN_TOPIC + " = " + topicName,
        null);
  }

  public List<Topic> getAllTopics() {
    List<Topic> topics = new ArrayList<Topic>();

     Cursor cursor = database.rawQuery("SELECT * FROM " + TopicDBHelper.TABLE_TOPICS, null);

    /*
     * Cursor cursor = database.query(TopicDBHelper.TABLE_TOPICS, allColumns, null, null, null,
     * null, null);
     */

    if (cursor.moveToFirst()) {
      while (!cursor.isAfterLast()) {
        Topic topic = cursorToTopic(cursor);
        Log.e("GET ALL TOPICS", "" + topic.get_topicName());
        topics.add(topic);
        cursor.moveToNext();
      }
      // make sure to close the cursor
      cursor.close();
    }
    return topics;
  }

  private Topic cursorToTopic(Cursor cursor) {
    Topic topic = new Topic();
    topic.set_topicName(cursor.getString(1));
    return topic;
  }

1 个答案:

答案 0 :(得分:0)

游标中的列索引从零开始。由于只有一列,getString(1)中的cursorToTopic()引用了不存在的列,并且可以返回null

改为将其更改为getString(0)

代码还有许多其他问题;覆盖它们可能对Stack Overflow来说太宽泛了。