Sri Soundhar Sri Soundhar - 1 month ago 11
Android Question

How to insert strings(Hard code) in multiple row's using primary key id in SQlite database Android

I create a table with three column(id,pooja,price).i need to insert some pooja's name in pooja column, which means multiple rows. the price will be the user input.
i don't know how to create and insert multiple rows.



public class DataBase_pooja_type extends SQLiteOpenHelper{

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "poojaManager";

// Pooja table name
private static final String TABLE_POOJA = "pooja";

// Pooja Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "poojatype";
private static final String KEY_PRICE = "price";


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

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_POOJA + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PRICE + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_POOJA);

// Create tables again
onCreate(db);
}




}

public class Setting extends AppCompatActivity {
Spinner sp;
EditText price;
Button save;
DataBase_pooja_type data;
ContentValues con;
String s1,s2,s3,s4;

int id_To_Update = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
sp = (Spinner) findViewById(R.id.sp3);
price = (EditText) findViewById(R.id.price);
save = (Button) findViewById(R.id.save);
data=new DataBase_pooja_type(Setting.this,"pooja",null,1);
con = new ContentValues();
s1 = "pooja 1";
s2 = "pooja 2";
s3 = "pooja 3";
s4 = "pooja 4";



}


}





my class file and database file code is inserted. and one more help i need to fetch that pooja names from data and set in spinner.

Answer

  String[] pooj = {
            "pooja 1",
            "pooja 2",
            "pooja 3",
            "pooja 4"
    };
 List<String> a = data.getAllLabels();
        if (a.size() == 0) {
            for (int i = 0; i<pooj.length; i++) {
                String num = pooj[i];
                con.put("poojatype",num);
                data.insert(con);
            }
        }

Comments