如何在我的SQL数据库android应用程序中获取我有多少colums(数据)的数量)

时间:2015-08-11 20:40:56

标签: android sqlite

我有dtabase

public DataAdapter(Context context){
    helper = new DataHelper(context);
}
public long insertData(String worB1, String worB2)
{
    SQLiteDatabase db=helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(helper.WORB1, worB1);
    contentValues.put(helper.WORB2, worB2);
    long id=db.insert(helper.TABLE_NAME, null, contentValues);
    return id;
}
public String getAllData()
{
    SQLiteDatabase db=helper.getWritableDatabase();
    String[] colums = {DataHelper.UID, DataHelper.WORB1, DataHelper.WORB2};
    Cursor cursor = db.query(DataHelper.TABLE_NAME, colums, null, null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while (cursor.getWantsAllOnMoveCalls())
    {
        int cid = cursor.getInt(0);
        String name = cursor.getString(1);
        String pass = cursor.getString(2);
        buffer.append(cid+ " "+name+ " "+pass+"\n");
    }
    return buffer.toString();
}
public String FindById(int id)
{
    SQLiteDatabase db=helper.getWritableDatabase();
    String[] colums = {DataHelper.WORB1, DataHelper.WORB2};
    Cursor cursor = db.query(DataHelper.TABLE_NAME, colums, DataHelper.UID + "='" + id + "'", null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while (cursor.moveToNext())
    {
        int index0 = cursor.getColumnIndex(DataHelper.WORB1);
        int index1 = cursor.getColumnIndex(DataHelper.WORB2);
        String personName = cursor.getString(index0);
        String pass = cursor.getString(index1);
        buffer.append(personName + " "+ pass + "\n");
    }
    return buffer.toString();
}
class DataHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "NekData";
    private static final String TABLE_NAME = "NekTable";
    private static final int DATABASE_VERSION = 1;
    private static final String UID = "_id";
    private static final String WORB1 = "worb1";
    private static final String WORB2 = "worb2";
    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + WORB1 + " VARCHAR(255), " + WORB2 + " VARCHAR(255));";
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
    private final Context context;
    public DataHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(CREATE_TABLE);
            Message.message(context, "All OK");
        } catch (SQLException e) {
            Message.message(context, "" + e);
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL(DROP_TABLE);
            onCreate(db);
    }
}

我想创建函数来获取我的数据库中有多少colums(id)的数量。 怎么做?

例如我添加了第一个colume == neka,pass; ecound colume == neka2,pass2,当我运行它的功能时,它让我的数字" 2"因为我有两个柱子

1 个答案:

答案 0 :(得分:0)

我不确定你在这里问的是什么,但如果你要求列计数它是一个常数,你可以通过在SqLiteDbHelper类中计算列(你实际上是自己定义的)来获得。

如果你想获得别的东西,请清楚你的问题,这很难理解。