我的DataHandler活动代码是
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataHandlerDairy {
public static final String DATE = "date";
public static final String DAIRY = "dairy";
private static final String DATA_BASE_NAME = "mydairy";
private static final String TABLE_NAME = "table_dairy";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_CREATE="create table table_diary (date text primary key," + "dairy text not null);";
DataBaseHelper1 dbhelper1;
Context ct;
SQLiteDatabase db;
public DataHandlerDairy(Context ct)
{
this.ct=ct;
dbhelper1 = new DataBaseHelper1(ct);
}
private static class DataBaseHelper1 extends SQLiteOpenHelper
{
public DataBaseHelper1(Context ct)
{
super(ct,DATA_BASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try
{
db.execSQL(TABLE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS table_dairy ");
onCreate(db);
}
}
public DataHandlerDairy open()
{
db = dbhelper1.getWritableDatabase();
return this;
}
public void close()
{
dbhelper1.close();
}
public long insertData(String date,String dairy)
{
ContentValues content = new ContentValues();
content.put(DATE, date);
content.put(DAIRY, dairy);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public Cursor returnData(String date1) throws SQLException
{
Cursor c = db.query(true, TABLE_NAME, new String[] { DATE, DAIRY}, DATE + "=" + date1, null, null, null, null, null);
if(c!=null)
{
c.moveToFirst();
}
return c;
}
}
当我尝试在DataHandler Activity中使用保存操作ie insertData()
方法时。并使用Intents存储日期来自另一个活动的日期。
将数据保存到数据库的代码是
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class Write_Dairy extends Activity {
EditText et1;
ImageButton correct,wrong;
DataHandlerDairy handler2;
String getdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write__dairy);
et1 = (EditText) findViewById(R.id.writediary);
correct=(ImageButton) findViewById(R.id.correct1);
wrong=(ImageButton) findViewById(R.id.wrong1);
Bundle bundle = getIntent().getExtras();
getdate = bundle.getString("message");
correct.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String getdairy = et1.getText().toString();
handler2 = new DataHandlerDairy(getBaseContext());
handler2.open();
long id = handler2.insertData(getdate,getdairy);
Toast.makeText(getBaseContext(), "your dairy is saved successfully", Toast.LENGTH_LONG).show();
handler2.close();
Intent ijk = new Intent(Write_Dairy.this,PickYourDate.class);
startActivity(ijk);
}
});
wrong.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getBaseContext(), "please write your dairy again", Toast.LENGTH_LONG).show();
et1.setText("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_write__dairy, menu);
return true;
}
}
并且为了重新获取数据我使用了日期作为主键,我在sql和java中都将它用作字符串。用于检索数据的代码是
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class Read_Dairy extends Activity {
String date1;
TextView tv1;
ImageButton imgb1;
Button bt1;
DataHandlerDairy handler3;
String getdata=" ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read__dairy);
tv1=(TextView) findViewById(R.id.readme);
imgb1=(ImageButton) findViewById(R.id.tick);
bt1=(Button) findViewById(R.id.ready);
Bundle bundle = getIntent().getExtras();
date1 = bundle.getString("kanna");
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler3 = new DataHandlerDairy(getBaseContext());
handler3.open();
Cursor c = handler3.returnData(date1);
if(c.moveToFirst())
{
do
{
getdata=c.getString(1);
}while(c.moveToNext());
}
handler3.close();
if(getdata==null)
{
tv1.setText("no data exists for this date");
}
else
{
tv1.setText(c.getString(1));
}
}
});
imgb1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent ks = new Intent(Read_Dairy.this,PickYourDate.class);
startActivity(ks);
}
});
Typeface font = Typeface.createFromAsset(getAssets(), "Strato-linked.ttf");
tv1.setTypeface(font);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_read__dairy, menu);
return true;
}
}
我试图存储数据时的堆栈跟踪是
E/SQLiteLog(16724): (1) no such table: table_dairy D/AndroidRuntime(16724): Shutting down VM E/AndroidRuntime(16724): FATAL EXCEPTION: main E/AndroidRuntime(16724): Process: simple.smile.my_dairy, PID: 16724 E/AndroidRuntime(16724): android.database.sqlite.SQLiteException: no such table: table_dairy (code 1): , while compiling: INSERT INTO table_dairy(date,dairy) VALUES (?,?) E/AndroidRuntime(16724): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) E/AndroidRuntime(16724): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) E/AndroidRuntime(16724): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) E/AndroidRuntime(16724): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) E/AndroidRuntime(16724): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) E/AndroidRuntime(16724): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) E/AndroidRuntime(16724): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469) E/AndroidRuntime(16724): at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1367) E/AndroidRuntime(16724): at simple.smile.my_dairy.DataHandlerDairy.insertData(DataHandlerDairy.java:79) E/AndroidRuntime(16724): at simple.smile.my_dairy.Write_Dairy$1.onClick(Write_Dairy.java:48) E/AndroidRuntime(16724): at android.view.View.performClick(View.java:4832) E/AndroidRuntime(16724): at android.view.View$PerformClick.run(View.java:19839) E/AndroidRuntime(16724): at android.os.Handler.handleCallback(Handler.java:739) E/AndroidRuntime(16724): at android.os.Handler.dispatchMessage(Handler.java:95) E/AndroidRuntime(16724): at android.os.Looper.loop(Looper.java:211) E/AndroidRuntime(16724): at android.app.ActivityThread.main(ActivityThread.java:5315) E/AndroidRuntime(16724): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(16724): at java.lang.reflect.Method.invoke(Method.java:372) AndroidRuntime(16724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) E/AndroidRuntime(16724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
请问有谁能说出我做错了什么,以及如何解决错误。 提前谢谢。
答案 0 :(得分:2)
您的表格名称不匹配:
private static final String TABLE_NAME = "table_dairy"; // DAIRY?????
和
private static final String TABLE_CREATE="create table table_diary (date text primary key," + "dairy text not null);";
此
return db.insertOrThrow(TABLE_NAME, null, content);
在您创建 table_diary 时尝试插入 table_dairy 。
因此,它正确地告诉您 table_dairy 不存在。