在 Fragment 中打开 Android Sqlite 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23073189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening Android Sqlite Database in Fragment
提问by user2466888
I'm trying to open and write to a Sqlite database in Android. According to the log output, it's not opening. I know that my syntax isn't great and I should be saving from a Party object instead of straight from the fragment interface, but my focus is really on the the process. Any help would be super helpful!
我正在尝试在 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);
}
}
And here is the interface screen:
这是界面屏幕:
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
}
}
}
回答by M D
Try this
尝试这个
SqlDatabase dbEntry = new SqlDatabase(getActivity());
instead of
代替
SqlDatabase dbEntry = new SqlDatabase(this);