2017-10-09 137 views
-3

当我单击“显示”按钮时,我的应用程序将崩溃。
我遵循指南,只是改变名称,但由于某种原因,它不断崩溃。当我尝试获取数据时,SQLite应用程序崩溃

请帮我理解为什么会发生这种情况。

这是我CustDbAdapter CODE:

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); 
} 

} 

,这是CustFragment调用Apapter。

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); 

      } 
     }); 
    } 
} 
+2

分享你的崩溃日志 –

+0

连接错误日志的问题 – akhilesh0707

回答

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)" 
相关问题