ssummeraw 2013-05-03 06:11 采纳率: 10%
浏览 14800
已采纳

如何从sqlite数据库中获取数据并显示在listview中?

在登录页面后,我想在listview中把Apple显示成A,Boy显示成B等等,直到F。但是在程序中当我完全登录后,只有登录表成功创建,主菜单还是没有创建。
我想在test database中创建主菜单,然后我想从主菜单表(mainmenu table)中获取数据再显示在listview中。
我使用了下面的代码:

 if(username.length()>0&&password.length()>0)
        {
            SQLiteAdapter db=new SQLiteAdapter(Main.this);
            db.openToWrite();
            if(db.Login(username,password))
            {
                System.out.println("goutham");
                Intent intent=new Intent(getApplicationContext(),ExampleActivity.class);

                startActivity(intent);
            } 

SQLiteAdapter.java

}

public Cursor queueAll() {
    String[] columns = new String[] { KEY_ID, KEY_CONTENT };
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
            null, null, null, null);

    return cursor;
}

private static class SQLiteHelper extends SQLiteOpenHelper {

    public SQLiteHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

public long AddUser(String username, String password) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_USERNAME, username);
    initialValues.put(KEY_PASSWORD, password);
    return sqLiteDatabase.insert(DATABASE_TABLE, null, initialValues);

}

public boolean Login(String username, String password) {
    // TODO Auto-generated method stub
    Cursor mCursor = sqLiteDatabase.rawQuery("SELECT * FROM "
            + DATABASE_TABLE + " WHERE username=? AND password=?",
            new String[] { username, password });
    if (mCursor != null) {
        if (mCursor.getCount() > 0) {
            return true;
        }
    }
    return false;
}

 }

ExampleActivity.java

 public class ExampleActivity extends Activity {
private SQLiteAdapter mySQLiteAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView listContent = (ListView) findViewById(R.id.contentlist);

    /*
     * Create/Open a SQLite database and fill with dummy content and close
     * it
     */
    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToWrite();
    // mySQLiteAdapter.deleteAll();

    mySQLiteAdapter.insert("A for Apply");
    mySQLiteAdapter.insert("B for Boy");
    mySQLiteAdapter.insert("C for Cat");
    mySQLiteAdapter.insert("D for Dog");
    mySQLiteAdapter.insert("E for Egg");
    mySQLiteAdapter.insert("F for Fish");

    mySQLiteAdapter.close();

    /*
     * Open the same SQLite database and read all it's content.
     */
    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToRead();

    Cursor cursor = mySQLiteAdapter.queueAll();
    startManagingCursor(cursor);

    String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
    int[] to = new int[] { R.id.text };

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
            R.layout.row, cursor, from, to);

    listContent.setAdapter(cursorAdapter);

    mySQLiteAdapter.close();

}
}

运行程序后,登录表在数据库中创建了,但是主菜单表没有创建。运行程序后,显示一个错误:

sqlite returned code=1 no such a table in MY_TABLE
  • 写回答

1条回答

  • Curie-87 2013-05-06 02:26
    关注

    Database class:

    public String getData1() throws SQLException{
            // TODO Auto-generated method stub
            String[] columns1 = new String[] { KEY_DATE };
            Cursor c1 = ourDatabase.query(DATABASE_MARKSTABLE, columns1, null, null, null,
                    null, KEY_ENDINGTIME+" DESC", " 30");
            String result1 = "";
    
            int isName = c1.getColumnIndex(KEY_DATE);
    
            for (c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()) {
    
                result1 = result1 + c1.getString(isName)
                        + "  " + "\n";
             }
            c1.close();
            return result1;
         }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示