jeudi 29 octobre 2015

Insert and extract with sqlite android

I'm trying to insert data into my database, once i click on add button ,app crash.

and how can i extract data and set it into text view like the labels

thank you in advance

Here is my code

MainActivity class

public class MainActivity extends AppCompatActivity {

DB db;
Button addmed,addpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DB(this);
}

public void addmedView(View view){
    Intent ADDMEDVIEW = new Intent(this,ADDMEDCINEVIEW.class);
    startActivity(ADDMEDVIEW);

}
public void addpplview(View view){
    Intent ADDPPLVIEWS = new Intent(this,ADDPPLVIEW.class);
    startActivity(ADDPPLVIEWS);
}}

ADDMEDCINEVIEW Class where I'm trying to insert the data

public class ADDMEDCINEVIEW extends Activity {
DB db;
EditText MEDNAME,MEDPORP,NOT;
Button ADDDATA;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addmedview);
    MEDNAME = (EditText)findViewById(R.id.mednamevalue);
    MEDPORP = (EditText)findViewById(R.id.purposevalue);
    NOT = (EditText)findViewById(R.id.nooftapvalue);
    ADDDATA = (Button)findViewById(R.id.ADDMEDDATA);
    addDATA();
}

public void addDATA(){
    ADDDATA.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            boolean isInserted = db.inserData(
                    MEDNAME.getText().toString(),
                    MEDPORP.getText().toString(),
                    NOT.getText().toString()
            );
            if(isInserted == true){
                Toast.makeText(ADDMEDCINEVIEW.this,"Inserted",Toast.LENGTH_LONG).show();
            }
            else
                Toast.makeText(ADDMEDCINEVIEW.this,"NOT INSERTED",Toast.LENGTH_LONG).show();
        }
    });

}}

DataBase Class

public class DB extends SQLiteOpenHelper {

public final static String DBNAME="MEDCINEDB.db";
public final static String Table_name="MEDCINETable";
public final static String col1="MEDCINEID";
public final static String col2="MEDCINENAME";
public final static String col3="MEDCINEPURPOSE";
public final static String col4="NOTAPLET";


public DB(Context context) {
    super(context, DBNAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + Table_name + "(MEDCINEID INTEGER PRIMARY KEY AUTOINCREMENT,MEDCINENAME TEXT,MEDCINEPURPOSE TEXT,NOTAPLET INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP IF EXISTS"+Table_name);
    onCreate(db);

}
public boolean inserData(String MEDCINENAME,String MEDCINEPURPOSE,String NOTAPLET){
    SQLiteDatabase db= this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(col2,MEDCINENAME);
    contentValues.put(col3,MEDCINEPURPOSE);
    contentValues.put(col4,NOTAPLET);

    long Result = db.insert(Table_name,null ,contentValues);
    if(Result == -1){
        return false;
    }
    else
        return true;
}}

Manifest

   <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ADDMEDCINEVIEW"></activity>
    <activity android:name=".ADDPPLVIEW"></activity>
</application>

Aucun commentaire:

Enregistrer un commentaire