当我尝试获取数据时SQLite应用程序崩溃

时间:2017-10-09 11:48:00

标签: android android-sqlite

当我点击“显示”按钮时,我的应用程序将崩溃 我正在关注一个指南,只是在这里和那里更改名称,但由于某种原因,它一直在崩溃。

请帮助我理解为什么会这样。

这是我的CustDbAdapter代码:

public class CustDbAdapter {

public static final String CUST_TABLE_NAME = "customers";
public static final String _ID = "_id";
public static final String CUSTLNAME = "custlname";
public static final String CUSTFNAME = "custfname";
public static final String CUSTPHONE = "custphone";
private static final String DATABASE_NAME = "cool.db";
static final int CUSTBASE_VERSION = 1;
private static final String DATABASE_CREATE =
        "CREATE TABLE " + CUST_TABLE_NAME + " ("
                + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT,  "
                + CUSTLNAME + " TEXT NOT NULL, "
                + CUSTFNAME + " TEXT NOT NULL, "
                + CUSTPHONE + " INTEGER NOT NULL, "

private CustDbHelper dbhelper;
private final Context context;
private SQLiteDatabase db;

public CustDbAdapter(Context c) {
    this.context = c;
    dbhelper = new CustDbHelper(context);
}

private  static  class CustDbHelper extends SQLiteOpenHelper {
    CustDbHelper(Context context){
        super(context, DATABASE_NAME, null, CUSTBASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        try{
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public  void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to"
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXIST " + CUST_TABLE_NAME);
        onCreate(db);
    }
}


public CustDbAdapter open() throws SQLException {

    dbhelper =  new CustDbHelper(context);
    db = dbhelper.getWritableDatabase();
    return this;
}


public void close(){
    dbhelper.close();
}

public long addCustomer(String lname, String fname, int phone) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(CUSTLNAME, lname);
    initialValues.put(CUSTFNAME, fname);
    initialValues.put(CUSTPHONE, phone);

    return db.insert(CUST_TABLE_NAME, null, initialValues);

}

public Cursor getAllCustomer(){
    return db.query(CUST_TABLE_NAME, new String[]{_ID, CUSTLNAME,
            CUSTFNAME, CUSTPHONE}, null, null, null, null, null);
}

}

这是调用Apapter的CustFragment。

public class CustFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_util_cust, container, false);
        return v;
    }

    @Override
    public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);

        final Button btnShow = (Button)v.findViewById(R.id.b_utilcust_input);
        final TextView txtOutput = (TextView)v.findViewById(R.id.t_utilcust_output);

        final CustDbAdapter db = new CustDbAdapter(getActivity());

        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.open();
                Cursor c = db.getAllCustomer();
                if (c.moveToFirst()){
                    txtOutput.setText("Name - Phone \n");
                    c.moveToNext();

                    do {
                        DisplayContact(c);
                    } while (c.moveToNext());
                }
                db.close();
            }

            private void DisplayContact(Cursor c){

                int idColumnIndex = c.getColumnIndex(CustDbAdapter._ID);
                int lnameColumnIndex = c.getColumnIndex(CustDbAdapter.CUSTLNAME);
                int fnameColumnIndex = c.getColumnIndex(CustDbAdapter.CUSTFNAME);
                int phoneColumnIndex = c.getColumnIndex(CustDbAdapter.CUSTPHONE);

                int currentID = c.getInt(idColumnIndex);
                String currentLName = c.getString(lnameColumnIndex);
                String currentFName = c.getString(fnameColumnIndex);
                int currentPhone = c.getInt(phoneColumnIndex);

                txtOutput.append("\n"
                        + currentID + " - " + currentLName +", "+currentFName +" Phone:" + currentPhone);

            }
        });
     }
}

1 个答案:

答案 0 :(得分:1)

注意你的语法。您的表格创建命令中有额外逗号 结束括号缺失。

private static final String DATABASE_CREATE =
    "CREATE TABLE " + CUST_TABLE_NAME + " ("
    + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT,  "
    + CUSTLNAME + " TEXT NOT NULL, "
    + CUSTFNAME + " TEXT NOT NULL, "
    + CUSTPHONE + " INTEGER NOT NULL, "

必须

private static final String DATABASE_CREATE =
    "CREATE TABLE " + CUST_TABLE_NAME + " ("
    + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT,  "
    + CUSTLNAME + " TEXT NOT NULL, "
    + CUSTFNAME + " TEXT NOT NULL, "
    + CUSTPHONE + " INTEGER NOT NULL)"