更新行而不是创建新的条目数据库android
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了更新行而不是创建新的条目数据库android,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含10035字,纯文字阅读大概需要15分钟。
内容图文
![更新行而不是创建新的条目数据库android](/upload/InfoBanner/zyjiaocheng/888/2b9603eb1ff04b839372689959f14cff.jpg)
我一直在研究Android食谱书中的食谱,以利用数据库来存储事件.当前代码允许我添加新条目,但是我无法修改任何添加的条目.我需要的是具有预定行数(48)的数据库,并具有通过相应的edittext字段更新这些行的功能.谁能帮我修改以下代码来实现这一目标?我是android编码的新手,我需要从此数据库开始.
这是我的MyDB文件:
package com.cookbook.data;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
public class MyDB {
private SQLiteDatabase db;
private final Context context;
private final MyDBhelper dbhelper;
// Initializes MyDBHelper instance
public MyDB(Context c){
context = c;
dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null,
Constants.DATABASE_VERSION);
}
// Closes the database connection
public void close()
{
db.close();
}
// Initializes a SQLiteDatabase instance using MyDBhelper
public void open() throws SQLiteException
{
try {
db = dbhelper.getWritableDatabase();
} catch(SQLiteException ex) {
Log.v("Open database exception caught", ex.getMessage());
db = dbhelper.getReadableDatabase();
}
}
// Saves a diary entry to the database as name-value pairs in ContentValues instance
// then passes the data to the SQLitedatabase instance to do an insert
public long insertdiary(String title, String content)
{
try{
ContentValues newTaskValue = new ContentValues();
newTaskValue.put(Constants.TITLE_NAME, title);
newTaskValue.put(Constants.CONTENT_NAME, content);
newTaskValue.put(Constants.DATE_NAME, java.lang.System.currentTimeMillis());
return db.insert(Constants.TABLE_NAME, null, newTaskValue);
} catch(SQLiteException ex) {
Log.v("Insert into database exception caught",
ex.getMessage());
return -1;
}
}
// Reads the diary entries from database, saves them in a Cursor class and returns it from the method
public Cursor getdiaries()
{
Cursor c = db.query(Constants.TABLE_NAME, null, null,
null, null, null, null);
return c;
}
}
这是我的MyDBhelper文件:
package com.cookbook.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MyDBhelper extends SQLiteOpenHelper{
private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "+
Constants.DATE_NAME+" long);";
// database initialization
public MyDBhelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v("MyDBhelper onCreate","Creating all the tables");
try {
db.execSQL(CREATE_TABLE);
} catch(SQLiteException ex) {
Log.v("Create table exception", ex.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
Log.w("TaskDBAdapter", "Upgrading from version "+oldVersion
+" to "+newVersion
+", which will destroy all old data");
db.execSQL("drop table if exists "+Constants.TABLE_NAME);
onCreate(db);
}
}
这是我的常量文件:
package com.cookbook.data;
public class Constants {
public static final String DATABASE_NAME="datastorage";
public static final int DATABASE_VERSION=1;
public static final String TABLE_NAME="diaries";
public static final String TITLE_NAME="title";
public static final String CONTENT_NAME="content";
public static final String DATE_NAME="recorddate";
public static final String KEY_ID="_id";
public static final String TABLE_ROW="row_id";
}
这是我的日记文件,可在数据库中创建新条目:
package com.example.classorganizer;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.cookbook.data.MyDB;
import com.cookbook.data.MyDBhelper;
public class Diary extends Activity {
EditText titleET1,contentET1;
EditText titleET2,contentET2;
Button submitBT;
MyDB dba;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diary);
dba = new MyDB(this);
dba.open();
titleET1 = (EditText)findViewById(R.id.diary1);
contentET1 = (EditText)findViewById(R.id.diarycontentText1);
titleET2 = (EditText)findViewById(R.id.diary2);
contentET2 = (EditText)findViewById(R.id.diarycontentText2);
submitBT = (Button)findViewById(R.id.submitButton);
submitBT.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
saveItToDB();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void saveItToDB() {
dba.insertdiary(titleET1.getText().toString(), contentET1.getText().toString());
dba.insertdiary(titleET2.getText().toString(), contentET2.getText().toString());
dba.close();
titleET1.setText("");
contentET1.setText("");
titleET2.setText("");
contentET2.setText("");
Intent i = new Intent(Diary.this, DisplayDiaries.class);
startActivity(i);
}
/** Called when the user clicks the Back button */
public void visitMonday(View view) {
Intent intent = new Intent(this, Monday.class);
startActivity(intent);
}
}
最后是我的DisplayDiaries文件,该文件在列表视图中返回创建的日记:
package com.example.classorganizer;
import java.util.Date;
import java.text.DateFormat;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;
public class DisplayDiaries extends ListActivity {
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary{
public MyDiary(String t, String c, String r){
title=t;
content=c;
recorddate=r;
}
public String title;
public String content;
public String recorddate;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MyDB(this);
dba.open();
setContentView(R.layout.diaries);
super.onCreate(savedInstanceState);
myAdapter = new DiaryAdapter(this);
this.setListAdapter(myAdapter);
}
private class DiaryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyDiary> diaries;
public DiaryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
diaries = new ArrayList<MyDiary>();
getdata();
}
public void getdata(){
Cursor c = dba.getdiaries();
startManagingCursor(c);
if(c.moveToFirst()){
do{
String title =
c.getString(c.getColumnIndex(Constants.TITLE_NAME));
String content =
c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
DateFormat dateFormat =
DateFormat.getDateTimeInstance();
String datedata = dateFormat.format(new
Date(c.getLong(c.getColumnIndex(
Constants.DATE_NAME))).getTime());
MyDiary temp = new MyDiary(title,content,datedata);
diaries.add(temp);
} while(c.moveToNext());
}
}
@Override
public int getCount() {return diaries.size();}
public MyDiary getItem(int i) {return diaries.get(i);}
public long getItemId(int i) {return i;}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.diaryrow, null);
holder = new ViewHolder();
holder.mTitle = (TextView)v.findViewById(R.id.name);
holder.mDate = (TextView)v.findViewById(R.id.datetext);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mdiary = getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
holder.mDate.setText(holder.mdiary.recorddate);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyDiary mdiary;
TextView mTitle;
TextView mDate;
}
}
/** Called when the user clicks the Back button */
public void visitDiary(View view) {
Intent intent = new Intent(this, Diary.class);
startActivity(intent);
}
}
正如我之前提到的,此代码在运行时允许创建新日记并将其放在列表视图中.我需要修改此代码,以便数据库具有预定义的48行(默认为空),并且日记文件允许通过相应的48个edittext字段来修改行.以上任何帮助将不胜感激.我期待着您的学习.干杯,帕特里克
编辑 – – – – – – – – – – – – – – – – – – – – – – – – – ————————————-
因为我是一个绝对的初学者,所以我仍然无法在表中创建默认的48行,然后编写代码以使用相应的edittext更新每一行.也许有一些乐于助人的灵魂可以为我解决这个问题?
编辑2 ———————————————— ———————————–
我已经用您的代码更新了MyDBhelper onCreate方法,如下所示:
@Override
public void onCreate(SQLiteDatabase db) {
Log.v("MyDBhelper onCreate","Creating all the tables");
ContentValues cv=new ContentValues();
cv.put(Constants.KEY_ID, 1);
cv.put(Constants.TITLE_NAME, "My App");
db.insert( Constants.TABLE_NAME, null, cv);
String Updatetable= "update" + Constants.TABLE_NAME +
"Set" + Constants.CONTENT_NAME + " = " + 1 +
"Where" +Constants.KEY_ID +" = " + R.id.diary1;
try {
db.execSQL(CREATE_TABLE);
} catch(SQLiteException ex) {
Log.v("Create table exception", ex.getMessage());
}
}
但是在Diary的onCreate上创建了新行,而不是更新现有行…在这里我在做什么错?我相信我将代码放置在错误的位置,或者错过了其他东西…
解决方法:
我不确定如何处理48个编辑文本,但是下面的示例将为您提供编写更新查询的想法.
String Updatetable= "update " + Constants.TABLE_NAME + "Set " + Constants.CONTENT_NAME + " = " + your value + "Where " +Constants.KEY_ID +" = " + <<May be your edit text value>> +
将上面的查询写在要更新值的位置
编辑 – – – – – – – – – – – – – – – – – – – – – – – – – ——————————————-
首先,您需要具有这些行应包含的内容的数据,然后才有多种将数据插入数据的方法.
>使用内容值
ContentValues cv = new ContentValues();
cv.put(Constants.KEY_ID,1);
cv.put(Constants.TITLE_NAME,“我的应用”);
db.insert(Constants.TABLE_NAME,null,cv);
>如上所述编写查询(更新查询)并使用任何方法rawquery,execsql
db.rawQuery(<<您的插入查询>> ;,空)
以同样的方式execsql
要获取更多信息,请检查android documentationable. link
内容总结
以上是互联网集市为您收集整理的更新行而不是创建新的条目数据库android全部内容,希望文章能够帮你解决更新行而不是创建新的条目数据库android所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。