· 7 years ago · Oct 19, 2018, 05:00 AM
1@Override
2 protected void onCreate(Bundle savedInstanceState) {
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.activity_main);
5 final Button thebutton=findViewById(R.id.serviceButton);
6 movieInput=findViewById(R.id.movieInput);
7 movieList=findViewById(R.id.movieList);
8 dbHandler=new MyDBHandler(this,null,null,1);
9 dateReleased=findViewById(R.id.dateReleasedTxt);
10 final Button addButton=findViewById(R.id.addButton);
11 final Button deleteButton=findViewById(R.id.deleteButton);
12 printDatabase();
13
14
15 thebutton.setOnClickListener(new View.OnClickListener() {
16 @Override
17 public void onClick(View v) {
18 Intent Intent1=new Intent(MainActivity.this,MyService.class);
19 startService(Intent1);
20 //Intent theIntent=new Intent(MainActivity.this,theIntentService.class);
21 //startService(theIntent);
22 }
23 });
24 addButton.setOnClickListener(new View.OnClickListener() {
25 @Override
26 public void onClick(View v) {
27 String fileName="/sdcard/.mpg";
28 Movies movie=new Movies(movieInput.getText().toString(),dateReleased.getText().toString(),fileName);
29 //will only accept a movie object
30 dbHandler.addMovie(movie);
31 printDatabase();
32 }
33
34 });
35 deleteButton.setOnClickListener(new View.OnClickListener() {
36 @Override
37 public void onClick(View v) {
38 String inputText=movieList.getText().toString();
39 dbHandler.deleteMovie(inputText);
40 }
41 });
42
43
44 }
45
46 public void printDatabase()
47 {
48 String dbString=dbHandler.databaseToString();
49 movieList.setText(dbString);
50 movieInput.setText("");
51 }
52
53private static final int DATABASE_VERSION=1;
54 private static final String DATABASE_NAME="movies.db";
55 public static final String TABLE_PRODUCTS="_movies";
56 public static final String COLUMN_TITLE="_title";
57 public static final String COLUMN_DATERELEASED="_dateReleased";
58 public static final String COLUMN_FILENAME="_fileName";
59
60 public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
61 super(context, name, factory, version);
62 }
63
64 @Override
65 public void onCreate(SQLiteDatabase db) {
66 //INFORMATION ABOUT EACH COLUMN FOR THE TABLE
67 String query="CREATE TABLE "+TABLE_PRODUCTS+"("+
68 COLUMN_TITLE+" TEXT, "+
69 COLUMN_DATERELEASED+"TEXT, "+
70 COLUMN_FILENAME+" TEXT "
71 +" ); ";
72 db.execSQL(query);
73 }
74
75 @Override
76 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
77 db.execSQL("DROP TABLE IF EXISTS "+TABLE_PRODUCTS);
78 onCreate(db);
79 }
80
81 //add a new row to the database
82 public void addMovie(Movies movie)
83 {
84 ContentValues values=new ContentValues();
85 values.put(COLUMN_TITLE,movie.get_name());
86 values.put(COLUMN_DATERELEASED,movie.get_dateReleased());
87 values.put(COLUMN_FILENAME,movie.get_fileName());
88 SQLiteDatabase db=getWritableDatabase();
89 db.insert(TABLE_PRODUCTS,null,values);
90 db.close();
91 }
92
932018-10-18 20:05:12.208 9852-9852/com.example.bryce.purnellassignment_5 E/SQLiteLog: (1) table _movies has no column named _dateReleased
942018-10-18 20:05:12.209 9852-9852/com.example.bryce.purnellassignment_5 E/SQLiteDatabase: Error inserting _dateReleased=Date Released _title= _fileName=/sdcard/.mpg
95 android.database.sqlite.SQLiteException: table _movies has no column named _dateReleased (code 1): , while compiling: INSERT INTO _movies(_dateReleased,_title,_fileName) VALUES (?,?,?)
96 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
97 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
98 at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
99 at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
100 at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
101 at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
102 at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1472)
103 at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
104 at com.example.bryce.purnellassignment_5.MyDBHandler.addMovie(MyDBHandler.java:47)
105 at com.example.bryce.purnellassignment_5.MainActivity$2.onClick(MainActivity.java:47)
106 at android.view.View.performClick(View.java:5637)
107 at android.view.View$PerformClick.run(View.java:22429)
108 at android.os.Handler.handleCallback(Handler.java:751)
109 at android.os.Handler.dispatchMessage(Handler.java:95)
110 at android.os.Looper.loop(Looper.java:154)
111 at android.app.ActivityThread.main(ActivityThread.java:6119)
112 at java.lang.reflect.Method.invoke(Native Method)
113 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
114 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
115
116COLUMN_DATERELEASED+"TEXT, "
117
118COLUMN_DATERELEASED+" TEXT, "
119
120android.database.sqlite.SQLiteException: table _movies has no column named _dateReleased
121
122public void onCreate(SQLiteDatabase db)
123 {
124 String query = "CREATE TABLE IF NOT EXISTS"
125 + TABLE_PRODUCTS + "("
126 + COLUMN_TITLE + " TEXT NOT NULL,"
127 + COLUMN_DATERELEASED + " TEXT NOT NULL,"
128 + COLUMN_FILENAME + " TEXT NOT NULL);";
129 db.execSQL(query);
130 }
131
132public void onCreate(SQLiteDatabase db) {
133 //INFORMATION ABOUT EACH COLUMN FOR THE TABLE
134 String query="CREATE TABLE "+TABLE_PRODUCTS+"("+
135 COLUMN_TITLE+" TEXT, "+
136 COLUMN_DATERELEASED+" TEXT, "+
137 COLUMN_FILENAME+" TEXT "
138 +" ); ";
139 db.execSQL(query);