我在表2中遇到错误,即在条目表中没有这样的表Entry_table当我将数据插入表时它转到Toast数据库错误错误链接将我带到我输入数据的入口表中 这是我的错误:
01-15 11:40:52.638 3420-3420/com.example.nitin.mithun E/SQLiteLog: (1) no such table: Entry_table
01-15 11:40:52.644 3420-3420/com.example.nitin.mithun E/SQLiteDatabase: Error inserting AMOUNT=2 CARET=2 BALANCE=2 AMOUNT_PAID=2
android.database.sqlite.SQLiteException: no such table: Entry_table (code 1): , while compiling: INSERT INTO Entry_table(AMOUNT,CARET,BALANCE,AMOUNT_PAID) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.nitin.mithun.DatabaseHelper.insertdatat(DatabaseHelper.java:75)
at com.example.nitin.mithun.Entry$1.onClick(Entry.java:45)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Entry.java
这是我的活动:
package com.example.nitin.mithun;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Entry extends AppCompatActivity {
Button btnsbmt1;
Spinner spiner2;
EditText milkcaret,amount, amountp,bal;
DatabaseHelper mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry);
mydb=new DatabaseHelper(this);
btnsbmt1=(Button)findViewById(R.id.btnsubmit2);
spiner2=(Spinner)findViewById(R.id.spinner2);
milkcaret=(EditText)findViewById(R.id.txt1_de);
amount=(EditText)findViewById(R.id.txt2_de);
amountp=(EditText)findViewById(R.id.txt3_de);
bal=(EditText)findViewById(R.id.txt4_de);
adddata2();
}
public void adddata2(){
btnsbmt1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean inserteddata = mydb.insertdatat(milkcaret.getText().toString(),amount.getText().toString(),amountp.getText().toString(),bal.getText().toString());
if (inserteddata)
Toast.makeText(Entry.this, "data inserted", Toast.LENGTH_SHORT).show();
else {
Toast.makeText(Entry.this, "database error", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
这是我的数据库助手:
package com.example.nitin.mithun;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Nitin on 12/16/2015.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="Sales.db";
public static final String TABLE_NAME1="Insert_table";
public static final String TABLE_NAME2="Entry_table";
public static final String COL_ID="ID";
public static final String Col_NAME="NAME";
public static final String Col_CARET="CARET";
public static final String Col_AMOUNT="AMOUNT";
public static final String Col_AMOUNT_PAID="AMOUNT_PAID";
public static final String Col_BALANCE="BALANCE";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME1 + " (ID_NAME INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT)");
db.execSQL("create table " + TABLE_NAME2 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,CARET INTEGER, AMOUNT INTEGER,AMOUNT_PAID INTEGER,BALANCE INTEGER,FOREIGN KEY(ID) REFERENCES Insert_table(ID_NAME))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME1);
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME2);
onCreate(db);
}
public boolean insertdata(String name){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues =new ContentValues();
contentValues.put(Col_NAME, name);
long result= db.insert(TABLE_NAME1,null,contentValues);
if(result== -1)
return false;
else
return true;
}
public boolean insertdatat(String caret,String amount,String amount_paid,String balance ){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues =new ContentValues();
contentValues.put(Col_CARET,caret);
contentValues.put(Col_AMOUNT,amount);
contentValues.put(Col_AMOUNT_PAID,amount_paid);
contentValues.put(Col_BALANCE,balance);
long result= db.insert(TABLE_NAME2,null,contentValues);
if(result== -1)
return false;
else
return true;
}
}
答案 0 :(得分:1)
首先卸载应用程序然后重新安装。然后这将顺利运行。