0

AndroidアプリでSqliteでテーブルを定義しましたが、複数の行を定義できないようです.. package my.first.pro;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DBAdapter {

public static final String KEY_NAME = "name";
public static final String KEY_EIGHTU = "8u";
public static final String KEY_NINEU = "9u";
public static final String KEY_TENU = "10u";
public static final String KEY_ELEVENU = "11u";

private static final String DB_NAME = "db_example";
private static final String DB_TABLE = "al amal";
private static final int    DB_VER = 1;

// a SQL statement to create a new table
private static final String DB_CREATE = 
    "CREATE TABLE al amal (name TEXT NOT NULL, 8u TEXT NOT NULL, 9u TEXT NOT NULL, 10u    TEXT NOT NULL, 11u TEXT NOT NULL);";


private static class DatabaseHelper extends SQLiteOpenHelper {

    // Class constructor
    DatabaseHelper(Context c) {
        super(c, DB_NAME, null, DB_VER);
    }

    // called by the parent class when a DB doesn't exist
    public void onCreate(SQLiteDatabase db) {
        // Execute our DB_CREATE statement
        db.execSQL(DB_CREATE);
    }


}


private final Context context;  
private DatabaseHelper helper;
private SQLiteDatabase db;

// DBAdapter class constructor
public DBAdapter(Context c) {
    this.context = c;
}


public DBAdapter open() throws SQLException {
    // instantiate a DatabaseHelper class (see above)
    helper = new DatabaseHelper(context);

            db = helper.getWritableDatabase();

    return this;
}


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


//String name, String eu, String nu, String tu, String elu
public long insertUser( ) {
    ContentValues vals = new ContentValues();

    //the following is filling the first row.. but i don't know how to fill out more than one row
    vals.put(KEY_NAME, "John Smith");
    vals.put(KEY_EIGHTU, "F");
    vals.put(KEY_NINEU, "F");
    vals.put(KEY_TENU, "NF");
    vals.put(KEY_ELEVENU, "NF");
    return db.insert(DB_TABLE, null, vals);
}



public void findavailabe (String name) {
// Perform a database query
Cursor cursor = db.query(
        DB_TABLE, // table to perform the query
        new String[] { KEY_NAME,KEY_EIGHTU,KEY_NINEU,KEY_TENU,KEY_ELEVENU }, //resultset columns/fields
        KEY_NAME+"=? AND "+KEY_EIGHTU+"=? AND"+KEY_NINEU+"=? AND "+KEY_TENU+"=? AND "+KEY_ELEVENU, //condition or selection
        new String[] { user, pass },  //selection arguments (fills in '?' above)
        null,  //groupBy
        null,  //having
        null   //orderBy
    );


/** Authenticate a user by querying the table to see
  * if that user and password exist. We expect only one row
  * to be returned if that combination exists, and if so, we
  * have successfully authenticated.
  * 
  * @param user username (string)
  * @param pass user's password (string)
  * @return true if authenticated, false otherwise
 */
/**public boolean authenticateUser(String user, String pass) {
    // Perform a database query
    Cursor cursor = db.query(
            DB_TABLE, // table to perform the query
            new String[] { KEY_USER }, //resultset columns/fields
            KEY_USER+"=? AND "+KEY_PASS+"=?", //condition or selection
            new String[] { user, pass },  //selection arguments (fills in '?' above)
            null,  //groupBy
            null,  //having
            null   //orderBy
        );

    // if a Cursor object was returned by the query and
    // that query returns exactly 1 row, then we've authenticated
    if(cursor != null && cursor.getCount() == 1) {
        return true;
    }

    // The query returned no results or the incorrect
    // number of rows
    return false;
}

}}

2 番目の質問: sqlite でクエリを実行するのに問題があります。私のテーブルは 5 つの列で構成されています。最初の列は名前で、残りの 4 列は予約時間です。マークされている特定の名前を検索したいです。無料で「F」として..

4

1 に答える 1

0

i can't seem to define more than one row

関数insertUser( )を変更して、行を定義する 5 つの値を渡すことができます。次に、その関数を複数回呼び出すと、複数の行が生成されます。

public long insertUser(String name, String eightU, String nineU, String tenU, String elevanU) {
   ContentValues vals = new ContentValues();

   vals.put(KEY_NAME, name);
   vals.put(KEY_EIGHTU, eightU);
   vals.put(KEY_NINEU, nineU);
   vals.put(KEY_TENU, tenU);
   vals.put(KEY_ELEVENU, elevanU);
   return db.insert(DB_TABLE, null, vals);
}
于 2012-12-31T12:26:17.267 に答える