package com.example.userdatabase;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String BC_TABLE = "bc";
private static final String TABLE_NAME = "supervisor";
private static final String COL_NAME = "name";
private static final String COL_PASS = "password";
private static String DATABASE_NAME = "USER_DATA";
private static int DATABASE_VER = 1;
/*
* here we create the "create table query" the spaces while writing the
* query must be used carefully, any miss of the space between the element
* of the query may give a serious exception/error
*/
private static final String BC_NAME = "bcName";
private static final String BC_ID = "bcId";
public static final String BCC_ID = "bccId";
private static final String TERMINAL_ID = "terminal";
private static final String CENSUS_CODE = "census";
private static final String BRANCH_CODE = "branch";
private static final String MOBILE_NO = "mobile";
private static final String ADHAAR_NO = "adhaar";
private static final String ACCOUNT_NO = "account";
private static final String BANK_RO = "bankRo";
private static final String PIN_CODE = "pinCode";
private static final String IFSC_CODE = "ifscCode";
private static final String BC_SUPERVISOR="connectiion";
private String CREATE = "CREATE TABLE " + TABLE_NAME + " (" + COL_NAME
+ "TEXT PRIMARY KEY , " + COL_PASS + " TEXT NOT NULL " + ");";
***
/*Second table create query*/
private static final String CREATE_BC = "CREATE TABLE " + BC_TABLE + " ("
+ BC_ID + " TEXT NOT NULL, " + BC_NAME + " TEXT NOT NULL, "
+ TERMINAL_ID + " INTEGER NOT NULL, " + BCC_ID
+ " INTEGER NOT NULL, " + CENSUS_CODE + " INTEGER NOT NULL, "
+ BRANCH_CODE + " INTEGER NOT NULL, " + MOBILE_NO
+ " INTEGER NOT NULL, " + ADHAAR_NO + " INTEGER NOT NULL, "
+ ACCOUNT_NO + " INTEGER NOT NULL, " + BANK_RO
+ " INTEGER NOT NULL, " + PIN_CODE + " INTEGER NOT NULL, "
+ IFSC_CODE + " INTEGER NOT NULL, " + BC_SUPERVISOR
+ " FOREIGN KEY (" + BC_SUPERVISOR + ")REFERENCES " + TABLE_NAME
+ "(" + COL_NAME + "));";
***
/*
* this is the constructor of DatabaseHandler class , having instance of
* context class as an argument
*/
public DatabaseHandler(Context context) {
// we implement the super class
super(context, DATABASE_NAME, null, DATABASE_VER);
Log.d("database created", "database ready");
// TODO Auto-generated constructor stub
}
/*
* this is the first time the database is written , this method takes object
* of SQLiteDatabase class as an argument,since execSQl is the method of
* SQLiteDatabase class,thus to use execSQL we need to instantiate the
* SQLiteDatabase class
*/
/* Avoid using getWritable() or getReadable() within onCreate() */
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE);
db.execSQL("PRAGMA foreign_keys=ON");
db.execSQL(CREATE_BC);
Log.d("table created", "tablecreated");
// TODO Auto-generated method stub
}
/* user defined method to insert the record into the table supervisor */
public void insertRecord(DatabaseHandler dbh, String name, String password)
throws SQLException {
SQLiteDatabase sdb = dbh.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DatabaseHandler.COL_NAME, name);
cv.put(DatabaseHandler.COL_PASS, password);
// can also use long id=sdb.insert(DatabaseHandler.TABLE_NAME, null,cv);
sdb.insert(DatabaseHandler.TABLE_NAME, null, cv);
Log.d("data insertion", "one row inserted");
}
/*
* here we make use of cursor class,make the query to the table to fetch
* both the columns of the table,and returns the cursor to the hosting
* activity then the cursor is moved to the whole result set performing
* various comparisions
*/
public Cursor checkdata(DatabaseHandler dbh) {
SQLiteDatabase db = dbh.getReadableDatabase();
String[] columns = { DatabaseHandler.COL_NAME, DatabaseHandler.COL_PASS };
Cursor CR = db.query(DatabaseHandler.TABLE_NAME, columns, null, null,
null, null, null);
Log.d("data retrieveing", "checkdata");
return CR;
}
/ *这里我使用insert方法将数据插入表* / public void insertBcData(DatabaseHandler dbh,String bcId,String name, String terminalId,String bccId,String censusCode, String branchCode,String mobileNo,String adhaar,String account, String bankRo,String pinCode,String ifscCode)抛出SQLException {
SQLiteDatabase db = dbh.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DatabaseHandler.BC_ID, Integer.parseInt(bcId));
cv.put(DatabaseHandler.BC_NAME, name);
cv.put(DatabaseHandler.TERMINAL_ID, Integer.parseInt(terminalId));
cv.put(DatabaseHandler.BCC_ID, Integer.parseInt(bccId));
cv.put(DatabaseHandler.CENSUS_CODE, Integer.parseInt(censusCode));
cv.put(DatabaseHandler.BRANCH_CODE, Integer.parseInt(branchCode));
cv.put(DatabaseHandler.MOBILE_NO, Integer.parseInt(mobileNo));
cv.put(DatabaseHandler.ADHAAR_NO, Integer.parseInt(adhaar));
cv.put(DatabaseHandler.ACCOUNT_NO, Integer.parseInt(account));
cv.put(DatabaseHandler.BANK_RO, Integer.parseInt(bankRo));
cv.put(DatabaseHandler.PIN_CODE, Integer.parseInt(pinCode));
cv.put(DatabaseHandler.IFSC_CODE, Integer.parseInt(ifscCode));
long bc = db.insert(DatabaseHandler.BC_TABLE,null, cv);
Log.d("Bc data", "bc data insertion done");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
第一个表CREATE的语法错误,将其更改为
private String CREATE = "CREATE TABLE " + TABLE_NAME + " (" + COL_NAME
+ "TEXT PRIMARY KEY");";
您还要将空值插入主键
sdb.insert(DatabaseHandler.TABLE_NAME, null, cv);