在Android

时间:2016-12-07 22:16:02

标签: android database sqlite android-studio

我试图通过按下按钮将两个字符串值保存到数据库中。当我按下按钮然后我的应用程序冻结。 我使用ADB shell来查看是否正在创建.db文件,它显示“找不到文件”的消息

调试应用程序时会显示以下日志:

I/System.out: waiting for debugger to settle...

I/System.out: waiting for debugger to settle...

I/System.out: debugger has settled (1378)

W/System: ClassLoader referenced unknown path: /data/app

/com.thebitshoes.thereaders-2/lib/arm

W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable

D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb


I/OpenGLRenderer: Initialized EGL, version 1.4

W/art: Suspending all threads took: 16.983ms

W/art: Suspending all threads took: 10.735ms

W/art: Suspending all threads took: 8.821ms

W/art: Suspending all threads took: 6.433ms

W/art: Suspending all threads took: 7.269ms

W/art: Suspending all threads took: 6.203ms
I/art: Background partial concurrent mark sweep GC freed 83(2960B) AllocSpace objects, 60(15MB) LOS objects, 39% free, 24MB/40MB, paused 6.654ms total 45.830ms


W/art: Suspending all threads took: 5.955ms

W/art: Suspending all threads took: 7.533ms

I/art: Background partial concurrent mark sweep GC freed 77(2752B) AllocSpace objects, 56(15MB) LOS objects, 40% free, 19MB/32MB, paused 8.008ms total 34ms

W/art: Suspending all threads took: 9.923ms

W/art: Suspending all threads took: 5.833ms

这是我的DBHandler java文件:

public class DBHandler extends SQLiteOpenHelper {

//Basic settings for database
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="thereaders.db";

//Name of the table in database
public static final String TABLE_NAME="newgoalentry";

//Name of the columns of the table
public static String COLUMN_ID="id";
public static String COLUMN_TITLE="goalTitle";
public static String COLUMN_DESCRIPTION="goalDescription";


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

@Override
public void onCreate(SQLiteDatabase db) {
    String query=" CREATE TABLE "+TABLE_NAME + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_TITLE + " TEXT," +
            COLUMN_DESCRIPTION + " TEXT " +
            ");";
    db.execSQL(query);
}

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

//this method will be called when CREATE button will be pressed for adding a new goal

public void addNewGoal(NewGoalEntry entry)
{
    try {
        ContentValues values = new ContentValues();
        values.put(COLUMN_TITLE, entry.getGoalTitle());
        values.put(COLUMN_DESCRIPTION, entry.getGoalDescription());

        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_NAME, null, values);
        db.close();
    }catch (Exception e)
    {
        e.printStackTrace();
    }


}

public String databaseToString()
{
    String dbString="";
    SQLiteDatabase db=getWritableDatabase();
    String query=" SELECT * FROM " +
            TABLE_NAME +
            " WHERE 1 ";

    Cursor c=db.rawQuery(query,null);
    c.moveToFirst();

    while(!c.isAfterLast()){
    if(c.getString(c.getColumnIndex("goalTitle"))!=null)
    {
        dbString+=c.getString(c.getColumnIndex("goalTitle"));
        dbString+="\n";
    }

}   db.close();
    return dbString;
}
}

这是活动java类

public class CreateNoteActivity extends Activity {

EditText goal_title;
EditText goal_description;
TextView db_result;
Button create_goal;
DBHandler dbHandler;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_note2);

    db_result=(TextView)findViewById(R.id.database_view);
    goal_title=(EditText) findViewById(R.id.goal_title);
    goal_description=(EditText) findViewById(R.id.goal_description);
    create_goal=(Button)findViewById(R.id.create_goal);
    dbHandler=new DBHandler(this,null,null,1);
    printvalues();

}

public void printvalues()
{
    String dbString=dbHandler.databaseToString();
    db_result.setText(dbString);
    goal_title.setText("");
    goal_description.setText("");
}

public void addEntry(View view){
    Toast.makeText(getBaseContext(),"New Reading Goal created !!",Toast.LENGTH_SHORT).show();
    NewGoalEntry entry=new NewGoalEntry(goal_title.getText().toString(),goal_description.getText().toString());
    dbHandler.addNewGoal(entry);
    printvalues();


}


}

1 个答案:

答案 0 :(得分:0)

尝试在每次读/写后删除db.close();。当我在我的一个应用程序上实现一个简单的数据库时,我认为我遇到了类似的问题。确保在您的应用程序不需要时关闭数据库,例如注销。