错误:03-26 23:19:58.913 12137-12137 / com.example.johnwalls.projet E / SQLiteLog:(1)没有这样的表:email1 03-26 23:19:58.913 12137-12137 / com.example.johnwalls.projet E / SQLiteDatabase:插入dem = hdhdhbbdbdjd时出错description = demande source=bechir@gmail.com
inboxFrgment.java
package com.example.johnwalls.projet;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.johnwalls.projet.R.id.pr;
/**
* A simple {@link Fragment} subclass.
*/
public class InboxFragment extends Fragment {
private Button sendB;
private EditText d;
private EditText des;
private DB base;
public InboxFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
base=new DB(getActivity());
final View view = inflater.inflate(R.layout.fragment_inbox, container, false);
TextView text2 = view.findViewById(R.id.in);//Find textview Id
String getArgument = getArguments().getString("main");
text2.setText(getArgument);
Button button = (Button) view.findViewById(R.id.log);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String getArgument = getArguments().getString("main");
EditText d= (EditText) view.findViewById(R.id.dem);
EditText des=(EditText) view.findViewById(R.id.desc);
String dem=d.getText().toString();
String desc=des.getText().toString();
Email e=new Email();
e.setSource(getArgument);
e.setDem(dem);
e.setDescription(desc);
base.insertEmail(e);
Toast.makeText(getActivity(), "Email Envoyé", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
DB.java
package com.example.johnwalls.projet;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by John Walls on 04-Mar-18.
*/
public class DB extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DB_nom="bef.db";
private static final String table_admin="admin";
private static final String table_email1="email1";
private static final String col_source="source";
private static final String col_description="description";
private static final String col_dem="dem";
private static final String col_cin="cin";
private static final String col_nom="nom";
private static final String col_pren="pren";
private static final String col_email="email";
private static final String col_password="password";
private static final String col_age="age";
SQLiteDatabase db;
private static final String creation_admin="create table admin (cin integer primary key,"+"nom text not null ,pren text not null ,email text not null,password text not null ,age integer not null);";
private static final String creation_email="create table email1 (source text primary key,"+"dem text not null ,description text not null);";
public DB(Context context) {
super(context,DB_nom, null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(creation_admin);
db.execSQL(creation_email);
this.db=db;
}
public void insertEmail(Email e)
{
db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(col_source,e.getSource());
values.put(col_dem,e.getDem());
values.put(col_description,e.getDescription());
db.insert(table_email1,null,values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query ="DROP TABLE IF EXISTS "+table_admin;
String query2="DROP TABLE IF EXISTS "+table_email1;
db.execSQL(query);
db.execSQL(query2);
this.onCreate(db);
}
}
你想要实现什么目标你想要什么?你得到了什么(包括错误信息)你还尝试了什么?您认为是什么造成的?为什么你需要为它提出一个新问题?为什么你的问题与其他类似的问题不同?
答案 0 :(得分:0)
我怀疑您在运行应用程序后添加了 email1 表,并且由于数据库帮助程序 onCreate
{}的常见误解而未创建该表无论何时创建Database Helper的实例(SQLiteOpenHelper的子类,即DB.java),都会运行strong>方法。
'onCreate'方法仅在创建数据库时运行一次。
您有三种方法可以解决此问题: -
删除应用程序的数据,然后重新运行应用程序。
卸载应用程序并重新运行应用程序。
在这种情况下,onUpgrade
方法将删除表,然后调用onCreate
,以增加数据库版本号(例如,将private static final int DATABASE_VERSION=1;
更改为{{ 1}})然后重新运行应用程序。