Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.thenewboston;
- import android.app.Activity;
- import android.app.Dialog;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class SqlLiteExample extends Activity implements OnClickListener{
- EditText etName,etHotness;
- Button btnSave,btnView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sqliteexample);
- etName=(EditText) findViewById(R.id.editText1);
- etName=(EditText) findViewById(R.id.editText2);
- btnSave=(Button) findViewById(R.id.btnSQLUPDATE);
- btnView=(Button) findViewById(R.id.btnSQLVIEW);
- btnSave.setOnClickListener(this);
- btnView.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch(v.getId()){
- case R.id.btnSQLUPDATE:
- boolean didItWork=true;
- try{
- String name=etName.getText().toString();
- String hotness=etHotness.getText().toString();
- HotOrNot entry= new HotOrNot(SqlLiteExample.this);
- entry.open();
- entry.createEntry(name,hotness);
- entry.close();
- }catch(Exception e){
- didItWork= false;
- }finally{
- if(didItWork){
- Dialog d= new Dialog(this);
- d.setTitle("Heck yea");
- System.out.println("testing");
- TextView tv= new TextView(this);
- tv.setText("sucess");
- d.setContentView(tv);
- d.show();
- }
- }
- break;
- case R.id.btnSQLVIEW:
- break;
- }
- }
- }
- package com.thenewboston;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class HotOrNot {
- public static final String KEY_ROWID = "_id";
- public static final String KEY_NAME = "persons_name";
- public static final String KEY_HOTNESS = "persons_hotness";
- private static final String DATABASE_NAME = "HotOrNotdb";
- private static final String DATABASE_TABLE = "peopleTable";
- private static final int DATABASE_VERSION = 1;
- private DbHelper ourhelper;
- private final Context ourContext;
- private SQLiteDatabase ourDatabase;
- private static class DbHelper extends SQLiteOpenHelper {
- public DbHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
- + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
- + " TEXT NOT NULL, " + KEY_HOTNESS + "TEXT NOT NULL);"
- );
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- db.execSQL("DROP TABLE IF EXIST "+ DATABASE_TABLE);
- onCreate(db);
- }
- }
- public HotOrNot(Context c) {
- ourContext = c;
- }
- public HotOrNot open() throws SQLException{
- ourhelper = new DbHelper(ourContext);
- ourDatabase= ourhelper.getWritableDatabase();
- return this;
- }
- public void close(){
- ourhelper.close();
- }
- public long createEntry(String name, String hotness) {
- // TODO Auto-generated method stub
- ContentValues cv= new ContentValues();
- cv.put(KEY_NAME, name);
- cv.put(KEY_HOTNESS, hotness);
- return ourDatabase.insert(DATABASE_TABLE, null, cv);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement