在片段中打开Android Sqlite数据库

我正在尝试在Android中打开并写入sqlite数据库.根据日志输出,它没有打开.我知道我的语法不是很好,我应该从Party对象中保存而不是直接从片段接口中保存,但是我的重点实际上是过程.任何帮助都会超级有帮助!

package com.android.sqldatabase.sqldatabaseapp;

//imports deleted to save space

public class sqlDatabase {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_PARTY = "party_date";
    public static final String KEY_STATE = "state";
public static final String KEY_JDATE = "j_date";
public static final String KEY_JTITLE = "j_title";
public static final String KEY_JENTRY = "j_entry";
public static final String KEY_Jrating = "j_rating";
    public static final String KEY_JENTRYEDITION = "j_entryEdition";

private static final String DATABASE_NAME = "PartyInfoDB";

    private static final String DATABASE_READABLE = "readableinformation";
private static final String DATABASE_WRITEABLE = "writeableinformation";
private static final int DATABASE_VERSION = 20141;

private DbHelper mHelper;

private final Context mContext;

private sqliteDatabase mDatabase;

private static class DbHelper extends sqliteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    //Set up database here
        public void onCreate(sqliteDatabase db) {
        db.execsql("CREATE TABLE " + DATABASE_READABLE + " (" + 
                 //Column name     Type of variable
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_PARTY + " TEXT NOT NULL, " +
                KEY_STATE + " TEXT NOT NULL);"

        db.execsql("CREATE TABLE " + DATABASE_WRITEABLE + " (" +
                KEY_JENTRYEDITION + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_JDATE + " INTEGER NOT NULL, " +
                KEY_JTITLE + " TEXT NOT NULL, " +
                KEY_Jrating + " FLOAT NOT NULL, " +
                KEY_JENTRY + " TEXT NOT NULL);"
                );

    }

    @Override
    public void onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {
        db.execsql("DROP TABLE IF EXISTS " + DATABASE_READABLE);
        db.execsql("DROP IF TABLE EXISTS " + DATABASE_WRITEABLE);
        onCreate(db);
    }

}

public sqlDatabase (Context c) {
    mContext = c;
}

public sqlDatabase open() throws sqlException {
    //Set up the helper with the context
    mHelper = new DbHelper (mContext);
    //Open the database with our helper
    mDatabase = mHelper.getWritableDatabase();
    return this;
}


    mHelper.close();
        }

public long createEntry(String title, String entry, float ratingBar,
        String date) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_JDATE, date);
    cv.put(KEY_JTITLE, title);
    cv.put(KEY_Jrating, ratingBar);
    cv.put(KEY_JENTRY, date);
    return mDatabase.insert(DATABASE_WRITEABLE, null, cv); 
}
}

这是界面屏幕:

package com.android.sqldatabase.sqldatabaseapp;

//imports deleted to save space

public class LogFragment extends Fragment implements OnClickListener{
    private String TAG;
private String TAG1;
private String mName;
EditText msqlTitle, msqlEntry;
Button mSaveButton;
ratingBar msqlratingBar;
private TextView msqlDate;
private Party mParty;
private Date mDate;
private Context mContext;

//Our onCreate method
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    }

//Inflate our fragment into the container
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle    
    savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_log, parent, false);

    mDate = new Date();
    msqlTitle = (EditText)v.findViewById(R.id.review_title);
    msqlEntry = (EditText)v.findViewById(R.id.review_entry_textBox);
    mSaveButton = (Button)v.findViewById(R.id.sqlsave_button);
    mSaveButton.setonClickListener(this);
    msqlratingBar = (ratingBar)v.findViewById(R.id.party_ratings_bar);
    msqlDate = (TextView)v.findViewById(R.id.review_date);

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    String sdfString = sdf.format(mDate);
    msqlDate.setText(sdfString);

    return v;
}

//Our 2 buttons
public void onClick(View arg0) {
switch (arg0.getId()) {
    case R.id.sqlsave_button:

        boolean didItWork = true;
        try {
        //Convert our edit texts to strings
        String  title = msqlTitle.getText().toString();
        String  entry = msqlEntry.getText().toString();
        float ratingBar = msqlratingBar.getrating();
        String date = msqlDate.getText().toString();

        //Write all of this to the database
        sqlDatabase dbEntry = new sqlDatabase(this);
        dbEntry.open();
        Log.i(TAG1, "Database opened.");
        dbEntry.createEntry(title, entry, ratingBar, date);
        dbEntry.close();

        } catch (Exception e) {
            didItWork = false;
            Log.e(TAG, "Database didn't upgrade when button pressed");
        } finally {
            if(didItWork) {
                Toast toast = Toast.makeText(getActivity(),   
                                    R.string.save_toast, Toast.LENGTH_LONG);
                    toast.show();
            }
    }

    break;
            R.id.cancelButton
            //INSET CANCEL BUTTON HERE
    }
}
}

解决方法:

尝试这个

 sqlDatabase dbEntry = new sqlDatabase(getActivity());

代替

 sqlDatabase dbEntry = new sqlDatabase(this);

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能