mardi 11 août 2015

How to get number of how much i have colums(data) in my SQL DataBase android app)

I have 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);
    }
}

and i wanna create function to get number of how much i have colums (id) in my database. How to do it ?

for example i am added first colume == neka, pass ; ecound colume == neka2, pass2 and when i run it function it get me numer "2" becouse i have two colums

Aucun commentaire:

Enregistrer un commentaire