2015-05-14 38 views
0

我一直試圖讓學生記錄 主鍵_id 作者 主題的數據庫 出版商的Android SQite數據庫搜索和查找colums錯誤

我的表不顯示任何列,它也崩潰 誤差addProduct命令 db.insert(TABLE_NAME,null,values);

這是數據庫類

package com.example.bilal.mydatabase; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class MyDBHandler extends SQLiteOpenHelper { 

    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "productDB.db"; 
    private static final String TABLE_NAME = "products"; 

// public static final String COLUMN_ID = "_id"; 
// public static final String COLUMN_PRODUCTNAME = "productname"; 
// public static final String COLUMN_QUANTITY = "quantity"; 
// public static final String COLUMN_QUANTITY = "quantity"; 
// public static final String COLUMN_QUANTITY = "quantity"; 


    public static final String COLUMN_ID = "_id"; 
// public static final String COLUMN_ISBN = "IsBN"; 
    public static final String COLUMN_AUTHOR = "AUTHOR"; 
    public static final String COLUMN_SUBJECT = "SUBJECT"; 
    public static final String COLUMN_PUBLISHER = "PUBLISHER"; 



    public MyDBHandler(Context context, String name,SQLiteDatabase.CursorFactory factory, int version) 
    { 
     super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
    } 

// @Override 
// public void onCreate(SQLiteDatabase db) { 
// 
// } 
// 
// @Override 
// public void onUpgrade(SQLiteDatabase db, int oldVersion, 
//       int newVersion) { 
// 
// } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +TABLE_NAME + "(" 
       + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_AUTHER + " TEXT," + COLUMN_SUBJECT + " TEXT," + COLUMN_PUBLISHER + " TEXT," + ")"; 
     db.execSQL(CREATE_PRODUCTS_TABLE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 

    public void addProduct(Product product) { 

     ContentValues values = new ContentValues(); 
     values.put(COLUMN_ID, product.getID()); 
     values.put(COLUMN_AUTHER, product.getAuther()); 
     values.put(COLUMN_SUBJECT, product.getSubject()); 
     values.put(COLUMN_PUBLISHER, product.getPublisher()); 
//  values.put(COLUMN_QUANTITY, product.getQuantity()); 

     SQLiteDatabase db = this.getWritableDatabase(); 

     db.insert(TABLE_NAME, null, values); 
     db.close(); 
    } 
    public Product findProduct(String id) { 
     String query = "Select * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + " = \"" + id + "\""; 

     SQLiteDatabase db = this.getWritableDatabase(); 

     Cursor cursor = db.rawQuery(query, null); 

     Product product = new Product(); 

     if (cursor.moveToFirst()) { 
      cursor.moveToFirst(); 
      product.setID(Integer.parseInt(cursor.getString(0))); 
      product.setAuther(cursor.getString(1)); 
      product.setSubject(cursor.getString(1)); 
      product.setPublisher(cursor.getString(1)); 
//   product.setSubject(Integer.parseInt(cursor.getString(2))); 
//   product.setPublisher(Integer.parseInt(cursor.getString(2))); 
      cursor.close(); 
     } else { 
      product = null; 
     } 
     db.close(); 
     return product; 
    } 

// public boolean deleteProduct(String productname) { 
// 
//  boolean result = false; 
// 
//  String query = "Select * FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " = \"" + productname + "\""; 
// 
//  SQLiteDatabase db = this.getWritableDatabase(); 
// 
//  Cursor cursor = db.rawQuery(query, null); 
// 
//  Product product = new Product(); 
// 
//  if (cursor.moveToFirst()) { 
//   product.setID(Integer.parseInt(cursor.getString(0))); 
//   db.delete(TABLE_PRODUCTS, COLUMN_ID + " = ?", 
//     new String[] { String.valueOf(product.getID()) }); 
//   cursor.close(); 
//   result = true; 
//  } 
//  db.close(); 
//  return result; 
// } 


} 

這是我的主要活動

package com.example.bilal.mydatabase; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends ActionBarActivity { 

// TextView idView; 
    EditText ID,AUTHEr,SUBJECt,PUBLISHEr; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     ID = (EditText) findViewById(R.id.Isbn); 
     AUTHEr = (EditText) findViewById(R.id.Auther); 
     SUBJECt = (EditText) findViewById(R.id.Subject); 
     PUBLISHEr = (EditText) findViewById(R.id.Publisher); 
//  productBox = (EditText) findViewById(R.id.productName); 
//  quantityBox = 
//    (EditText) findViewById(R.id.productQuantity); 
    } 

    public void Clearlog(View v) 
    { 

     AUTHEr.setText(""); 
     SUBJECt.setText(""); 
     PUBLISHEr.setText(""); 
     ID.setText(""); 


    } 


    public void newProduct (View view) { 
     MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1); 

//  int quantity = 
//    Integer.parseInt(quantityBox.getText().toString()); 
// 
//  Product product = 
//    new Product(productBox.getText().toString(), quantity); 


     int _id =Integer.parseInt(ID.getText().toString()); 
     String AuTHER =(AUTHEr.getText().toString()); 
     String SuBJECT =(SUBJECt.getText().toString()); 
     String PuBLISHER =(PUBLISHEr.getText().toString()); 


     Product a = new Product(_id,AuTHER,SuBJECT,PuBLISHER); 
//  Product s = new Product(SUBJECt.getText().toString(), quantity); 
//  Product p = new Product(PUBLISHEr.getText().toString(), quantity); 


     dbHandler.addProduct(a); 

     AUTHEr.setText(""); 
     SUBJECt.setText(""); 
     PUBLISHEr.setText(""); 
     ID.setText(""); 
     Toast.makeText(getApplicationContext(), "BOOK ADDED TO LIBRARY", Toast.LENGTH_LONG).show(); 
    } 

    public void lookupProduct (View view) { 
     MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1); 

     Product product =dbHandler.findProduct(ID.getText().toString()); 

     if (product != null) { 
//   idView.setText(String.valueOf(product.getID())); 

      ID.setText(String.valueOf(product.getID())); 
      AUTHEr.setText(String.valueOf(product.getAuther())); 
      SUBJECt.setText(String.valueOf(product.getSubject())); 
      PUBLISHEr.setText(String.valueOf(product.getPublisher())); 

     } else { 
      Toast.makeText(getApplicationContext(), "BOOK NOT FOUND", Toast.LENGTH_LONG).show(); 
     } 
    } 

// public void removeProduct (View view) { 
//  MyDBHandler dbHandler = new MyDBHandler(this, null, 
//    null, 1); 
// 
//  boolean result = dbHandler.deleteProduct(
//    productBox.getText().toString()); 
// 
//  if (result) 
//  { 
//   idView.setText("Record Deleted"); 
//   productBox.setText(""); 
//   quantityBox.setText(""); 
//  } 
//  else 
//   idView.setText("No Match Found"); 
// } 


} 

這是logcat的

05-14 18:09:16.414 6973-6973/com.example.bilal.mydatabase E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.bilal.mydatabase, PID: 6973 
    java.lang.IllegalStateException: Could not execute method of the activity 
      at android.view.View$1.onClick(View.java:3969) 
      at android.view.View.performClick(View.java:4637) 
      at android.view.View$PerformClick.run(View.java:19422) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5586) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at android.view.View$1.onClick(View.java:3964) 
            at android.view.View.performClick(View.java:4637) 
            at android.view.View$PerformClick.run(View.java:19422) 
            at android.os.Handler.handleCallback(Handler.java:733) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5586) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE products(_id INTEGER PRIMARY KEY,AuTHER TEXT,SuBJECT TEXT,PuBLISHER TEXT,) 
      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113) 
      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690) 
      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1806) 
      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1737) 
      at com.example.bilal.mydatabase.MyDBHandler.onCreate(MyDBHandler.java:49) 
      at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) 
      at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 
      at com.example.bilal.mydatabase.MyDBHandler.addProduct(MyDBHandler.java:68) 
      at com.example.bilal.mydatabase.MainActivity.newProduct(MainActivity.java:63) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at android.view.View$1.onClick(View.java:3964) 
            at android.view.View.performClick(View.java:4637) 
            at android.view.View$PerformClick.run(View.java:19422) 
            at android.os.Handler.handleCallback(Handler.java:733) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5586) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
            at dalvik.system.NativeStart.main(Native Method) 

回答

2

你可以改變你創建表查詢

String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_AUTHER + " TEXT," + COLUMN_SUBJECT + " TEXT," + COLUMN_PUBLISHER + " TEXT," + ")";

String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +TABLE_NAME + " (" 
        + COLUMN_ID + " INTEGER PRIMARY KEY, " + COLUMN_AUTHER + " TEXT, " + COLUMN_SUBJECT + " TEXT, " + COLUMN_PUBLISHER + " TEXT);"; 
+2

錯誤是','前')' –

+0

它finnaly workded –

+0

但我所有的箱子都出現了只什麼在auther –