Sqlite

时间:2017-01-03 09:42:36

标签: android database sqlite android-sqlite

我希望在SqlLite数据库中创建三个表。

第一个表( databaset_table )创建正常并且正常工作。

第二个表格( databaset_table_bedehkara n)和第三个表格( moshtarian_table )将无法创建。

我更新到最新版本,但我仍然无法弄明白。

package com.example.projectt;
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;
public class MainDataBase{
database_p m,mx;
public MainDataBase(Context context) {
    m=new database_p(context);
}
public Cursor GetAllData(){
    SQLiteDatabase db=m.getReadableDatabase();
    Cursor res=db.rawQuery("select * from "+ database_p.Table_name,null);
    return res;
}
public Cursor GetAllDatax(){
    SQLiteDatabase db=m.getReadableDatabase();
    Cursor res=db.rawQuery("select * from "+ database_p.Table_name2,null);
    return res;
}
public Cursor GetAllData_table3(){
    SQLiteDatabase db=mx.getReadableDatabase();
    Cursor res=db.rawQuery("select * from "+ database_p.Table3_name,null);
    return res;
}
//Select * from clients where Concat(field1, '', field2, '', fieldn) like "%Mary%"
public Cursor SearchData(String id){
    SQLiteDatabase db=m.getReadableDatabase();
    //Cursor res=db.rawQuery("select * from "+ database_p.Table_name +" where namex like %milad%",null);
    //String id1='%'+id+'%';
    Cursor res=db.rawQuery("select * from "+ database_p.Table_name +" where namex = ? or datex = ? or costx = ? or chekserialx = ? or accountnumberx = ? or bankx = ?",new String[] { id,id,id,id,id,id });
    //Cursor res=db.rawQuery("select * from "+ database_p.Table_name +" where concat(namex, '', datex, '', costx, '', chekserialx, '', accountnumberx, '', bankx) like %milad% " , null);
    return res;
}
public Cursor SearchDatax(String id){
    SQLiteDatabase db=m.getReadableDatabase();
    //Cursor res=db.rawQuery("select * from "+ database_p.Table_namex +" where namex like %milad%",null);
    //String id1='%'+id+'%';
    //Cursor res=db.rawQuery("select * from "+ database_p.Table_name +" where namex = ? or datex = ? or costx = ? or chekserialx = ? or accountnumberx = ? or bankx = ?",new String[] { id,id,id,id,id,id });
    //Cursor res=db.rawQuery("select * from "+ database_p.Table_name +" where concat(namex, '', datex, '', costx, '', chekserialx, '', accountnumberx, '', bankx) like %milad% " , null);
    Cursor res=db.rawQuery("select * from "+ database_p.Table_name2 +" where namexx = ? or costxx = ? or datexx = ?",new String[] { id,id,id });
    return res;
}
public boolean UpdateData(String name2,String date2,String cost2,String chekserial2,String accountnumber2,String bank2){
    SQLiteDatabase db=m.getReadableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(database_p.UID, name2);
    contentValues.put(database_p.Col1, date2);
    contentValues.put(database_p.Col2, cost2);
    contentValues.put(database_p.Col3, chekserial2);
    contentValues.put(database_p.Col4, accountnumber2);
    contentValues.put(database_p.Col5, bank2);
    db.update(database_p.Table_name,contentValues,"namex = ?",new String[] { name2 });
    return true;
}
public boolean UpdateDatax(String name2,String cost2,String date2){
    SQLiteDatabase db=m.getReadableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(database_p.UID2, name2);
    contentValues.put(database_p.Colx1, date2);
    contentValues.put(database_p.Colx2, cost2);;
    db.update(database_p.Table_name2,contentValues,"namex2 = ?",new String[] { name2 });
    return true;
}
public Integer DeleteData(String id1){
    SQLiteDatabase db=m.getReadableDatabase();
    return db.delete(database_p.Table_name,"namex =?",new String[] {id1});
}
public Integer DeleteDatax(String id1){
    SQLiteDatabase db=m.getReadableDatabase();
    return db.delete(database_p.Table_name2,"namex2 =?",new String[] {id1});
}
public long InsertData(String name3,String date3,String cost3,String chekserial3,String accountnumber3,String bank3){
    SQLiteDatabase db=m.getWritableDatabase();
    ContentValues contentValues=new ContentValues();;
    contentValues.put(database_p.UID, name3);
    contentValues.put(database_p.Col1, date3);
    contentValues.put(database_p.Col2, cost3);
    contentValues.put(database_p.Col3, chekserial3); 
    contentValues.put(database_p.Col4, accountnumber3);
    contentValues.put(database_p.Col5, bank3);
    Long result=db.insert(database_p.Table_name,null,contentValues );
    return result;
    }
public long InsertDatax(String name3,String cost3,String date3){
    SQLiteDatabase db=m.getWritableDatabase();
    ContentValues contentValues=new ContentValues();;
    contentValues.put(database_p.UID2, name3);
    contentValues.put(database_p.Colx1, cost3);
    contentValues.put(database_p.Colx2, date3);
    Long result=db.insert(database_p.Table_name2,null,contentValues );
    return result;
    }

DataBaseHelper

public class  database_p extends SQLiteOpenHelper{  
public static final String Database_name="database"; 
public static final String Table_name="databaset_table";  
public static final String Table_name2="databaset_table_bedehkaran";
public static final String Table3_name="moshtarian_table";  
public static final String UID="namex"; 
public static final String Col1="datex"; 
public static final String Col2="costx"; 
public static final String Col3="chekserialx"; 
public static final String Col4="accountnumberx";  
public static final String Col5="bankx";  
public static final String UID2="namex2";
public static final String Colx1="costx2";
public static final String Colx2="datex2";
public static final String T3_UID="name_m";
public static final String T3_Col1="email_m";
public static final String T3_Col2="telnum_m";
public static final String T3_Col3="phonenum_m";
public static final String T3_Col4="address_m";
private Context context;
public database_p(Context context) {
    super(context, Database_name,null, 5);
    this.context=context;
    // TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    try{
db.execSQL("CREATE TABLE "+Table_name+"(namex TEXT PRIMARY KEY,datex TEXT,costx TEXT,chekserialx TEXT,accountnumberx TEXT,bankx TEXT)" );
db.execSQL("CREATE TABLE "+Table_name2+"(namex2 TEXT PRIMARY KEY,costx2 TEXT,datex2 TEXT)" );
db.execSQL("CREATE TABLE "+Table3_name+"(name_m TEXT PRIMARY KEY,email_m TEXT,telnum_m INTEGER,phonenum_m INTEGER,address_m TEXT)" );
    }catch(SQLException e){
    Message.message(context,""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
    // TODO Auto-generated method stub
    try{
    db.execSQL("DROP TABLE IF EXISTS "+Table_name);
    db.execSQL("DROP TABLE IF EXISTS "+Table_name2);
    db.execSQL("DROP TABLE IF EXISTS "+Table3_name);
    onCreate(db);
    }catch(SQLException e){
        Message.message(context,""+e);
    }
   }
  }
}

1 个答案:

答案 0 :(得分:0)

作为全局变量,编写此代码:

private static final String CREATE_TABLE_NAME = "CREATE TABLE " + Table_name + "(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL1 + " TEXT," + COL2 + " TEXT," + COL3 + " TEXT" + ")";

private static final String CREATE_ TABLE_NAME_2 = "CREATE TABLE " + Table_name2 + "(" + UID + " TEXT PRIMARY KEY," + COL4 + " TEXT," + COL5 + " TEXT," + COL6 + " TEXT" + ")";

像这样编辑你的onCreate

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(CREATE_TABLE_NAME);
}