jeudi 30 juillet 2015

Insert data to database SQLite

I want to insert data to a database I have created. I have created an insert command at the OnCreate class. When viewing the database in SQLite Browser the data do not show. Can anyone help me

DatabaseHelper

package app.mobiledevicesecurity;

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

/**
* Created by Martin on 2015/07/28.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "mobilesec.db";
public static final String TABLE_NAME = "read_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "CATEGORY";
public static final String COL_3 = "HEADING";
public static final String COL_4 = "INFO";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY    AUTOINCREMENT, CATEGORY TEXT, HEADING TEXT, INFO TEXT)");
    ContentValues cv = new ContentValues();
    cv.put(COL_1,1);
    cv.put(COL_2,"Malware");
    cv.put(COL_3,"Virus");
    cv.put(COL_4,"Harmfull for device");
    db.insert(TABLE_NAME, null, cv);



}

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

MainActivity

 package app.mobiledevicesecurity;

 import android.content.Intent;
 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.Button;


public class MainActivity extends ActionBarActivity {
DatabaseHelper myDb;
private static Button readbtn;
private static Button quizbtn;
private static Button scoresbtn;
private static Button settingsbtn;
private static Button helpbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDb = new DatabaseHelper(this);
     OnClickReadButtonListener();
     OnClickQuizButtonListener();
     OnClickScoresButtonListener();
     OnClickSettingsButtonListener();
     OnClickHelpButtonListener();


}

public void OnClickReadButtonListener() {
    readbtn = (Button) findViewById(R.id.readbutton);
    readbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new    Intent("app.mobiledevicesecurity.Read_Category");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickQuizButtonListener() {
    quizbtn = (Button) findViewById(R.id.quizbutton);
    quizbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickScoresButtonListener() {
    scoresbtn = (Button) findViewById(R.id.scoresbutton);
    scoresbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Scores");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickSettingsButtonListener() {
    settingsbtn = (Button) findViewById(R.id.settingsbutton);
    settingsbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Settings");
                    startActivity(intent);
                }
            }
    );
}

public void OnClickHelpButtonListener() {
    helpbtn = (Button) findViewById(R.id.helpbutton);
    helpbtn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("app.mobiledevicesecurity.Help");
                    startActivity(intent);
                }
            }
    );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Aucun commentaire:

Enregistrer un commentaire