使用getter和setter

时间:2017-09-18 18:34:23

标签: android sqlite android-sqlite

我正在创建一个将数据插入数据库的应用程序。该表已创建,但没有任何内容插入到数据库中,我没有收到任何错误。我试图逐行查看代码,但我找不到任何错误。当我点击保存按钮时,它意外关闭。

在TreatmentRepo类中插入方法

        public void insert(Treatment treatment) {

    //Open connection to write data
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    String query = "select * from Treatment";
    Cursor cursor = db.rawQuery(query, null);

    int  count = cursor.getCount();

    values.put(Treatment.KEY_TreatmentID, count);
    values.put(Treatment.KEY_TreatmentNumber, treatment.getEditTextTreatmentNumber());
    values.put(Treatment.KEY_TreatmentType, treatment.getEditTextTreatmentType());
    values.put(Treatment.KEY_TreatmentName, treatment.getEditTextTreatmentName());

    db.insert(Treatment.TABLE, null, values);
    db.close(); // Closing database connection

}

这是我的onClick方法

 public void onClick(View v) {
    if (v.getId() == R.id.btnSave) {
        TreatmentRepo repo = new TreatmentRepo(this);


        editTextTreatmentName = (EditText) findViewById(R.id.editTextTreatmentName);
        editTextTreatmentType = (EditText) findViewById(R.id.editTextTreatmentType);
        editTextTreatmentNumber = (EditText) findViewById(R.id.editTextTreatmentNumber);
        btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(this);

        int treatmentNumber = Integer.parseInt(editTextTreatmentNumber.getText().toString());
        String treatmentType = editTextTreatmentType.getText().toString();
        String treatmentName = editTextTreatmentName.getText().toString();

        Treatment treatment = new Treatment();
        treatment.setEditTextTreatmentNumber(treatmentNumber);
        treatment.setEditTextTreatmentName(treatmentName);
        treatment.setEditTextTreatmentType(treatmentType);

        repo.insert(treatment);

        Toast.makeText(this, "New Treatment Insert", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, view_treatment_entry.class));
}
}

治疗

public class Treatment {


    // Labels table name
    public static final String TABLE = "Treatment";

    // Labels Table Columns names
    public static final String KEY_TreatmentID = "TreatmentId";
    public static final String KEY_TreatmentName = "TreatmentName";
    public static final String KEY_TreatmentType = "TreatmentType";
    public static final String KEY_TreatmentNumber = "TreatmentNumber";

    // property help us to keep data
    public int treatment_ID;
    public String editTextTreatmentName;
    public String editTextTreatmentType;
    public int editTextTreatmentNumber;


    public int getEditTextTreatmentNumber() {
        return editTextTreatmentNumber;
    }

    public void setEditTextTreatmentNumber(int editTextTreatmentNumber) {
        this.editTextTreatmentNumber = editTextTreatmentNumber;
    }

    public String getEditTextTreatmentName() {
        return editTextTreatmentName;
    }

    public void setEditTextTreatmentName(String editTextTreatmentName) {
        this.editTextTreatmentName = editTextTreatmentName;
    }

    public String getEditTextTreatmentType() {
        return editTextTreatmentType;
    }

    public void setEditTextTreatmentType(String editTextTreatmentType) {
        this.editTextTreatmentType = editTextTreatmentType;
    }
}

XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:context="com.instinctcoder.sqlitedb.StudentDetail$PlaceholderFragment"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Treatment Name"
            android:id="@+id/treatmentName"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="30dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Treatment Type"
            android:id="@+id/treatmentType"
            android:layout_below="@+id/treatmentName"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="29dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Treatment Number"
            android:id="@+id/treatmentNumber"
            android:layout_below="@+id/treatmentType"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="29dp" />

        <EditText
            android:id="@+id/editTextTreatmentName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/treatmentType"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/treatmentName"
            android:ems="10"
            android:inputType="textShortMessage" />

        <EditText
            android:id="@+id/editTextTreatmentType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/treatmentNumber"
            android:layout_alignEnd="@+id/editTextTreatmentName"
            android:layout_alignRight="@+id/editTextTreatmentName"
            android:layout_toRightOf="@+id/treatmentName"
            android:ems="10"
            android:inputType="textShortMessage" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editTextTreatmentNumber"
            android:layout_alignBottom="@+id/treatmentNumber"
            android:layout_alignLeft="@+id/editTextTreatmentType"
            android:layout_alignStart="@+id/editTextTreatmentType"
            android:layout_alignRight="@+id/editTextTreatmentType"
            android:layout_alignEnd="@+id/editTextTreatmentType" />

        <Button
            android:id="@+id/btnSave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:onClick="onClick"
            android:layout_toLeftOf="@+id/btnClose"
            android:text="Save" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Close"
            android:id="@+id/btnClose"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delete"
            android:id="@+id/btnDelete"
            android:layout_alignTop="@+id/btnSave"
            android:layout_toLeftOf="@+id/btnSave" />

    </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

我们缺少一些重要信息来帮助您解决问题。在没有猜测和检查的情况下,需要做一些事情来深究这一点。

  • 您的SQLiteOpenHelper类以及用于确认正确使用上下文的新片段
  • 您的Create SQL语句,以确保所期望的数据类型与您插入的方式匹配int,String,int,String。
  • 您的数据库是否打开正确返回数据库
  • 您的第一个Select查询是否正确返回计数

但如果没有这些信息,我会说你可以尝试一些事情。 首先查看插入是否与硬编码值一起使用以消除坏值的可能性并消除由于第一次查询而导致DB不可用的可能性。所以我会注释掉rawQuery和硬编码插入,然后用不同的方法硬编码选择all以查看它是否正确进入。

此外,您没有使用插入主ID,也不确定这是否是故意的。

我现在能为你做的最好,向你展示一个关于表格和SQL开放的通用管理的工作示例,因为我不喜欢为每个项目制作开场工具所以我想出了一个我制作的A35SQLUtil库以此目的。 A35是我个人公司的缩写App Studio 35。

随意采取以下任何一个示例并与它们一起运行,或尝试上述想法进行故障排除或提供其他信息。希望这有帮助。

很多时候我会创建一个需要调用者上下文的单例SQLOpenHelper,然后是特定表元素的dataContext,当然还有TableModel来表示用于管理它的列和SQL脚本。插入看起来像:

public class OrderDataContext {

/*///////////////////////////////////////////////////////////////
// MEMBERS
*////////////////////////////////////////////////////////////////
private static final String TAG = Globals.SEARCH_STRING + OrderDataContext.class.getSimpleName();


/*///////////////////////////////////////////////////////////////
// CONSTRUCTOR
*////////////////////////////////////////////////////////////////
private OrderDataContext(){}


/*///////////////////////////////////////////////////////////////
// CRUD OPERATIONS
*////////////////////////////////////////////////////////////////
public static long insertNewOrder(OrderModel model, Context context){
    long primaryKey = -1;
    SQLiteDatabase db = null;

    try{
        db = A35DBHelper.openDatabase(context);
        ContentValues initialValues = new ContentValues();

        initialValues.put(OrdersTable.COLUMN_ORDER_NUMBER, model.getID());
        initialValues.put(OrdersTable.COLUMN_VIN, model.getVIN());
        initialValues.put(OrdersTable.COLUMN_CREATED_AT_DATE,  model.getCreatedAtDate());
        initialValues.put(OrdersTable.COLUMN_IMAGE_COUNT, model.getImageCount());
        initialValues.put(OrdersTable.COLUMN_DEFAULT_THUMBNAIL_URL, model.getDefaultThumbnailUrl());

        primaryKey = db.insert(OrdersTable.TABLE_NAME, null, initialValues);

    } catch (Exception ex) {
        A35Log.e(TAG, "Failed to insert order: " + ex.getMessage());

    }

    A35DBHelper.closeDatabase(db);
    return  primaryKey;
}

} }

SQLHelper看起来像:

public final class A35DBHelper extends SQLiteOpenHelper {
private static final String TAG = A35DBHelper.class.getSimpleName();
private static int sDatabaseVersion;
private static String sDatabaseName = null;
private static List<Class<? extends IA35Table>> sTables;

private A35DBHelper(Context context) {
    super(context.getApplicationContext(), sDatabaseName, (CursorFactory)null, sDatabaseVersion);
}

public static void initialize(String databaseName, int databaseVersion, Class<? extends IA35Table> table) {
    ArrayList tables = new ArrayList();
    tables.add(table);
    initialize(databaseName, databaseVersion, (List)tables);
}

public static void initialize(String databaseName, int databaseVersion, List<Class<? extends IA35Table>> tables) {
    sDatabaseName = databaseName;
    sDatabaseVersion = databaseVersion;
    sTables = tables;
}

public void onCreate(SQLiteDatabase sqLiteDatabase) {
    Iterator i = sTables.iterator();

    while(i.hasNext()) {
        Class table = (Class)i.next();

        try {
            sqLiteDatabase.execSQL(((IA35Table)table.newInstance()).getCreateTableScript());
        } catch (Exception var5) {
            Log.e(TAG, var5.getMessage());
        }
    }

}

public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    Iterator i = sTables.iterator();

    while(i.hasNext()) {
        Class table = (Class)i.next();

        try {
            sqLiteDatabase.execSQL(((IA35Table)table.newInstance()).getUpgradeTableScript());
        } catch (Exception var7) {
            Log.e(TAG, var7.getMessage());
        }
    }

}

public static SQLiteDatabase openDatabase(Context context) throws Exception {
    return getA35DBHelper(context).getWritableDatabase();
}

public static void closeDatabase(SQLiteDatabase db) {
    CloseableUtil.close(TAG, db);
}

public static Cursor executeSelectQueryForCursor(Context context, String sqlScript) throws Exception {
    SQLiteDatabase readableDatabase = getA35DBHelper(context).getReadableDatabase();
    Cursor cursor = readableDatabase.rawQuery(sqlScript, (String[])null);
    return cursor;
}

public static void executeWriteQuery(Context context, String sqlScript) throws Exception {
    SQLiteDatabase writeSqLiteDatabase = getA35DBHelper(context).getWritableDatabase();
    writeSqLiteDatabase.execSQL(sqlScript);
    CloseableUtil.close(TAG, writeSqLiteDatabase);
}

public static void closeCursor(String tag, Closeable closeable) {
    CloseableUtil.close(tag, closeable);
}

private static A35DBHelper getA35DBHelper(Context context) throws Exception {
    try {
        return new A35DBHelper(context);
    } catch (Exception var2) {
        throw new Exception("MUST call A35DBHelper.initialize before using the helper methods");
    }
}

}

然后我的表模型看起来像:

public class OrdersTable implements IA35Table {

/*///////////////////////////////////////////////////////////////
// MEMBERS
*////////////////////////////////////////////////////////////////
private static final String TAG = Globals.SEARCH_STRING + OrdersTable.class.getSimpleName();
public static final String TABLE_NAME = "ordersTable";
public static final String COLUMN_PRIMARY_ID = "_id";
public static final String COLUMN_ORDER_NUMBER = "orderNumber";
public static final String COLUMN_VIN = "vin";
public static final String COLUMN_CREATED_AT_DATE = "createdAtDate";
public static final String COLUMN_IMAGE_COUNT = "imageCount";
public static final String COLUMN_DEFAULT_THUMBNAIL_URL = "defaultThumbnailUrl";
public static String columns[] = {
        OrdersTable.COLUMN_PRIMARY_ID,
        OrdersTable.COLUMN_REPAIR_ORDER_NUMBER,
        OrdersTable.COLUMN_CREATED_AT_DATE
};


/*///////////////////////////////////////////////////////////////
// IA35TABLE OVERRIDES
*////////////////////////////////////////////////////////////////
@Override
public String getCreateTableScript() {
    A35SQLHelper.CreateTableStatement createTableStatement = new A35SQLHelper.CreateTableStatement(TABLE_NAME);

    createTableStatement.addColumnAsIntPrimaryKey(COLUMN_PRIMARY_ID);
    createTableStatement.addColumn(A35SQLHelper.ColumnType.TEXT, COLUMN_REPAIR_ORDER_NUMBER, false, null);
    createTableStatement.addColumn(A35SQLHelper.ColumnType.TEXT, COLUMN_VIN, false, null);
    createTableStatement.addColumn(A35SQLHelper.ColumnType.TEXT, COLUMN_CREATED_AT_DATE, false, null);
    createTableStatement.addColumn(A35SQLHelper.ColumnType.INTEGER, COLUMN_IMAGE_COUNT, false, null);
    createTableStatement.addColumn(A35SQLHelper.ColumnType.TEXT, COLUMN_DEFAULT_THUMBNAIL_URL, false, null);

    String createTableScript = createTableStatement.generateSQL();
    A35Log.d(TAG, "Creating " + TABLE_NAME + " Table. Query: " + createTableScript);

    return createTableScript;

}
@Override
public String getUpgradeTableScript() {
    //Currently, I am dropping date and create the table again. But If required in future,
    //I will need to handle the data migration operations.
    StringBuilder sb = new StringBuilder();

    sb.append("DROP TABLE IF EXISTS " + TABLE_NAME + "; ");
    sb.append(getCreateTableScript());
    A35Log.d(TAG, "Dropping " + TABLE_NAME + " Table. Query: " + sb.toString());

    return sb.toString();

}

}

最后,我一般在应用程序文件中新建我的实例,如下所示:

public class A35Application extends Application {

/*///////////////////////////////////////////////////////////////
// MEMBERS
*////////////////////////////////////////////////////////////////
private final static String TAG = Globals.SEARCH_STRING + A35Application.class.getSimpleName();
private static final String A35_ORDER_DB_NAME = "a35_orders_db";
private static final int DB_VERSION = 1;


/*///////////////////////////////////////////////////////////////
// LIFECYCLE OVERRIDES
*////////////////////////////////////////////////////////////////
@Override
public void onCreate() {
    super.onCreate();
    Log.v(TAG, "onCreate()");
    setup();

}


/*//////////////////////////////////////////////////////////
// PUBLIC METHODS
*///////////////////////////////////////////////////////////
public void setup() {
    A35Log.v(TAG, "setup()");
    setCurrentEnvironment(A35Environment.Env.PRD);
    setupCustomFonts();
    **setupDatabase();**
    setupLogging();
    startAnalyticsReporting();
    if(!BuildConfig.DEBUG) {
        startCrashReporting();

    }

}


/*///////////////////////////////////////////////////////////////
// PRIVATE METHODS
*////////////////////////////////////////////////////////////////
private void setupDatabase(){
    A35Log.v(TAG, "setupDatabase()");
    A35DBHelper.initialize(A35_ORDERS_DB_NAME, DB_VERSION, getDBTables());

}
private ArrayList<Class<? extends IA35Table>> getDBTables() {
    ArrayList<Class<? extends IA35Table>> tables = new ArrayList<>();

    tables.add(SessionTable.class);
    tables.add(ServicesTable.class);
    tables.add(OrdersTable.class);
    tables.add(ImagesToUploadTable.class);
    tables.add(VideosToUploadTable.class);
    tables.add(AudiosToUploadTable.class);

    return tables;
}

}

然后,这里的彻底性是界面

public interface IA35Table {
 String getCreateTableScript();
 String getUpgradeTableScript();
}

并关闭util

public class CloseableUtil {
public CloseableUtil() {
}

public static void close(@NonNull String tag, @Nullable Closeable closeable) {
    if(closeable != null) {
        try {
            closeable.close();
        } catch (IOException var3) {
            Log.e(tag, "Unable to close: " + closeable, var3);
        }
    }

}

public static void closeWithThrowIOException(@Nullable Closeable closeable) throws IOException {
    if(closeable != null) {
        closeable.close();
    }

}

}