· 8 years ago · Jul 27, 2018, 10:04 PM
1how to solve no such table while inserting exception in sqlite data base
2I/Database(958): sqlite returned: error code = 1, msg = no such table: Exercise
3
4long d= dbAdapter.SaveExecise(date, exercise_Time, ex_Name, minutes, burned_cals);
5
6public class DBAdapter {
7
8 private static final String DB_NAME = "MYDB";
9 private static final int DB_VERSION = 1;
10 private static final String EXERCISE_TABLE_NAME = "Exercise";
11 private static final String TAG = "DBAdapter";
12 private DatabaseHelper DBHelper;
13 private SQLiteDatabase db;
14 private Context context;
15
16 // table Exercise columns name
17 public static final String KEY_DATE = "Date";
18 public static final String KEY_TIME = "Time";
19 public static final String KEY_NAME = "Name";
20 public static final String KEY_PERIOD = "Period";
21 public static final String KEY_BURNEDCALS = "Burned_Calories";
22
23 private static final String EXERCISE_TABLE_CREATE =
24 "create tables Exercise (Date text not null , "
25 + "Time text not null ,Name text not null,"
26 + "Period REAL not null, Burned_Calories REAL not null,"
27 + " primary key(Date,Time ) );";
28
29 public DBAdapter(Context ctxt) {
30 this.context = ctxt;
31 DBHelper = new DatabaseHelper(context);
32 }
33
34 private static class DatabaseHelper extends SQLiteOpenHelper {
35
36 DatabaseHelper(Context context) {
37 super(context, DB_NAME, null, DB_VERSION);
38 }
39
40 public void onCreate(SQLiteDatabase db) {
41 try {
42 db.execSQL(EXERCISE_TABLE_CREATE);
43 } catch (SQLException e) {
44 e.printStackTrace();
45 }
46 }
47
48 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
49 Log.w(TAG, "Upgrading database from version"
50 + oldVersion + " to " + newVersion
51 + ", which will destroy all old data");
52 db.execSQL("DROP TABLE IF EXISTS Exercise");
53 onCreate(db);
54 }
55
56 public DBAdapter open() throws SQLException {
57 db = DBHelper.getWritableDatabase();
58 return this;
59 }
60
61 // ---closes the database---
62 public void close() {
63 DBHelper.close();
64 }
65
66 // ---insert Exercise info to the Exercise table---
67 public long SaveExecise(String date, String time, String name, float period,
68 float BurnedCalories) {
69 ContentValues content = new ContentValues();
70 content.put(KEY_DATE, date);
71 content.put(KEY_TIME, time);
72 content.put(KEY_NAME, name);
73 content.put(KEY_PERIOD, period);
74 content.put(KEY_BURNEDCALS, BurnedCalories);
75
76 return db.insert(EXERCISE_TABLE_NAME, null, content);
77 }
78 }
79}
80
81save_exercise_btn = (Button) findViewById(R.id.save_exercise_Btn);
82save_exercise_btn.setOnClickListener(new View.OnClickListener() {
83 public void onClick(View v) {
84 showSavingDialog();
85 // save the name,ime,burnedcals of the exercise in the DB
86 int year, month, day;
87 year = localCalendar.get(1);
88 month = localCalendar.get(2) + 1;
89 day = localCalendar.get(5);
90 String date = year + "/" + month + "/" + day;
91 DBAdapter dbAdapter = new DBAdapter(SelectedExerciseInfo.this);
92 dbAdapter = dbAdapter.open();
93 long d = dbAdapter.SaveExecise(date, exercise_Time, ex_Name, minutes, burned_cals);
94 dbAdapter.close();
95 hideSavingDialog();
96 }
97});
98
99private static final String EXERCISE_TABLE_CREATE ="create tables Exercise (Date text not null , "+
100 "Time text not null ,Name text not null," + "Period REAL not null,
101 Burned_Calories REAL not null," +" primary key(Date,Time ) );" ;
102
103private static final String EXERCISE_TABLE_CREATE ="create table Exercise (Date text not null , "+
104 "Time text not null ,Name text not null," + "Period REAL not null,
105 Burned_Calories REAL not null," +" primary key(Date,Time ) );" ;
106
107public class DBAdapter
108{
109 private static final String DB_NAME="MYDB3131";
110 private static final int DB_VERSION= 1 ;
111 private static final String EXERCISE_TABLE_NAME="Exercise";
112 private static final String TAG = "DBAdapter";
113 private DatabaseHelper DBHelper;
114 private SQLiteDatabase db;
115 private Context context;
116 // table Exercise columns name
117 public static final String KEY_DATE="Date";
118 public static final String KEY_TIME="Time";
119 public static final String KEY_NAME="Name";
120 public static final String KEY_PERIOD="Period";
121 public static final String KEY_BURNEDCALS="Burned_Calories";
122 /*private static final String EXERCISE_TABLE_CREATE ="create table Exercise ( Date text not null , "+
123 "Time text not null ,Name text not null," + "Period REAL not null, Burned_Calories REAL not null );" ;
124
125*/
126 private static final String EXERCISE_TABLE_CREATE ="create table Exercise (Date text not null , "+
127 "Time text not null ,Name text not null," + "Period REAL not null, Burned_Calories REAL not null," +" primary key(Date,Time ) );" ;
128
129public DBAdapter(Context ctxt)
130 { this.context=ctxt;
131 DBHelper= new DatabaseHelper(context);
132 }
133 private static class DatabaseHelper extends SQLiteOpenHelper
134 {
135 DatabaseHelper(Context context)
136 {
137 super(context, DB_NAME, null, DB_VERSION);
138 }
139 @Override
140 public void onCreate(SQLiteDatabase db)
141 {
142 db.execSQL(EXERCISE_TABLE_CREATE);
143 System.out.println("hereeeee");
144 }
145 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
146 {
147 Log.w(TAG, "Upgrading database from version" + oldVersion +" to "+ newVersion + ", which will destroy all old data");
148 db.execSQL("DROP TABLE IF EXISTS Exercise");
149 onCreate(db);
150 }
151 }
152
153 //--open the DB
154 public DBAdapter open() throws SQLException
155 {
156 db = DBHelper.getWritableDatabase();
157 return this;
158 }
159
160 //---closes the database---
161 public void close()
162 {
163 DBHelper.close();
164 }
165 //---insert Exercise info to the Exercise table---
166 public long SaveExecise(String date ,String time,String name ,float period, float BurnedCalories)
167 {
168 ContentValues content = new ContentValues();
169 content.put(KEY_DATE, date);
170 content.put(KEY_TIME, time);
171 content.put(KEY_NAME, name);
172 content.put(KEY_PERIOD,period);
173 content.put(KEY_BURNEDCALS, BurnedCalories);
174
175 return db.insert(EXERCISE_TABLE_NAME, null, content);
176 }}
177
178public class MyA extends Activity {
179@Override
180protected void onCreate(Bundle savedInstanceState) {
181 // TODO Auto-generated method stub
182 super.onCreate(savedInstanceState);
183 setContentView(R.layout.main);
184 int year,month,day;
185 Calendar localCalendar = Calendar.getInstance();
186 year = localCalendar.get(1);
187 month = localCalendar.get(2)+1;
188 day = localCalendar.get(5);
189 String date= year +"/"+month+"/"+day;
190 DBAdapter dbAdapter=new DBAdapter(this);
191 dbAdapter=dbAdapter.open();
192 long d= dbAdapter.SaveExecise(date, 10034+"", 100+"", 100f, 100f);
193 dbAdapter.close();
194Toast.makeText(this, "text"+d, Toast.LENGTH_SHORT).show();
195}
196}