1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.example.programmingknowledge.sqliteapp;
import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;
/** * Created by ProgrammingKnowledge on 4/3/2015. */ public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "Student.db"; public static final String TABLE_NAME = "student_table"; public static final String COL_1 = "ID"; public static final String COL_2 = "NAME"; public static final String COL_3 = "SURNAME"; public static final String COL_4 = "MARKS";
public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); }
@Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)"); }
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); onCreate(db); }
public boolean insertData(String name,String surname,String marks) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COL_2,name); contentValues.put(COL_3,surname); contentValues.put(COL_4,marks); long result = db.insert(TABLE_NAME,null ,contentValues); if(result == -1) return false; else return true; } }
|